Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]Sprawdzenie $_GET
Forum PHP.pl > Forum > Przedszkole
arekk85
Albo ja coś źle robie, a może czegoś nie rozumiem....
Mam prosty formularz
  1. <div class="container">
  2. <div class="col-sm-10">
  3. <form action="zapisz.php" method="get" id="my_form" class="form-horizontal ">
  4. <div class="form-group">
  5. <label class="col-sm-2 control-label">Imię</label>
  6. <div class="col-sm-10">
  7. <input name="imie" type="text" class="form-control" placeholder="Imię">
  8. </div>
  9. </div>
  10. <div class="form-group">
  11. <label class="col-sm-2 control-label">Nazwisko</label>
  12. <div class="col-sm-10">
  13. <input name="nazwisko" type="text" class="form-control" placeholder="Nazwisko">
  14. </div>
  15. </div>
  16. <div class="form-group">
  17. <label class="col-sm-2 control-label">E-mail</label>
  18. <div class="col-sm-10">
  19. <input name="email" type="text" class="form-control" placeholder="E-mail">
  20. </div>
  21. </div>
  22.  
  23. <button type="submit" class="btn btn-default" >Zapisz</button>
  24. </form>
  25. </div>
  26. </div>


I teaz bardzo proste sprawdznie czy tablica $_GET jest pusta
  1. <?php
  2. if(empty($_GET)){
  3. echo "Błąd_1";
  4. }
  5. if(isset($_GET)){
  6. echo "Błąd_2";
  7. }
  8.  
  9. if(!isset($_GET)){
  10. echo "Błąd_3";
  11. }
  12.  
  13. if(!empty($_GET)){
  14. echo "Błąd_4";
  15. }
  16. ?>

Problem jest taki że nie waże czy wypełnie dane w formularzu czy nie zawsze wyświetli się "Błąd_2" "Błąd_4" dlaczego?
KsaR
  1. if (isset($_GET['email']))


Itd. Mozesz tez to grupowac typu:

  1. if (isset($_GET['email'],$_GET['nazwisko']))
  2. {
  3. }


Hm, a get jest domyslnie gdy przegladasz strone.
To samo gdy wysyla sie posta, jest domyslnie post..

if ($_GET)
..
--
isset($_GET)
sprawdzi czy istnieje -> czyli true.

!empty($_GET) czy $_GET cos zawiera, zrob print_r($_GET); co zwraca tongue.gif
Pyton_000
jak da isset($var1, $var2)
to muszę istnieć OBIE zmienne.
arekk85
Rozwiązałem tak ten proble:
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>HTML</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <!-- Bootstrap -->
  8. <link href="css/bootstrap.css" rel="stylesheet" type="text/css" media="screen">
  9. <link href="css/style.css" rel="stylesheet" type="text/css"/>
  10.  
  11. </head>
  12. <body>
  13. <?php
  14. $host = 'localhost';
  15. $user = 'arek';
  16. $password = 'arek';
  17. $database = 'arek';
  18. $con = mysqli_connect($host,$user,$password,$database) or die("Some error occurred during connection ");
  19. /*
  20.   $tabela="CREATE TABLE dane (
  21. id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  22. firstname VARCHAR(30) NOT NULL,
  23. lastname VARCHAR(30) NOT NULL,
  24. email VARCHAR(50),
  25. reg_date TIMESTAMP
  26. ) ";
  27.  
  28.   mysqli_query($con, $tabela);
  29.   */
  30. ?>
  31.  
  32. <?php include_once 'menu.inc'; ?>
  33.  
  34. <div class="container">
  35. <?php
  36. $imie = filter_input(INPUT_GET, 'imie');
  37. $nazwisko = filter_input(INPUT_GET, 'nazwisko');
  38. $email = filter_input(INPUT_GET, 'email');
  39.  
  40. $imie = preg_replace('/[^a-zA-Z0-9-+ ]/', '', $imie);
  41. $nazwisko= preg_replace('/[^a-zA-Z0-9-+ ]/', '', $nazwisko);
  42. $emailo= preg_replace('/[^a-zA-Z0-9-+ ]/', '', $email);
  43.  
  44. if (empty($imie) || empty($nazwisko) || empty($email)) {
  45. echo "Bład";
  46. } elseif (!empty($imie) || !empty($nazwisko) || !empty($email)) {
  47. $dodaj="INSERT INTO dane (firstname,lastname,email) VALUES ("."\"".$imie."\"".","."\"".$nazwisko."\"".","."\"" .$email."\"". ")";
  48. mysqli_query($con, $dodaj);
  49. }
  50. ?>
  51.  
  52.  
  53. </div>
  54.  
  55. <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
  56. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  57. <!-- Include all compiled plugins (below), or include individual files as needed -->
  58. <script src="js/bootstrap.min.js"></script>
  59. </body>
  60. </html>
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.