简体   繁体   中英

What am i doing wrong in this PHP example?

i've been working on php with w3schools for 2 days and i've tried to rewrite the form example without looking the example code in there. But when i click the submit button, i can't get the results. I get the errors properly but when i fill the input fields correctly, page is just renewig itself and nothing happens. I compare mine with the original codes in w3schools but i didin't notice any mistakes. What am i doing wrong here?

<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title></title>
</head>
<body>

  <?php
    $name = $gender = $email = "";
    $nameErr = $genderErr = $emailErr = "";

    if($_SERVER["REQUEST_METHOD"] == "POST"){
      if(empty($_POST["name"])){
        $nameErr = "Name cannot be empty.";
      }
      else{
        $name = test_input($name);
        if(!preg_match("/^[a-zA-Z ]*$/",$name)){
          $nameErr = "The name is not valid, name cannot contain special characters.";
        }
      }
      if(empty($_POST["email"])){
        $emailErr = "E-Mail cannot be empty.";
      }
      else{
        $email = test_input($email);
        if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
          $emailErr = "The E-Mail is not valid";
        }
      }
      if(empty($_POST["gender"])){
        $genderErr = "Gender is required.";
      }
      else{
        $gender = test_input($gender);
      }
    }

    function test_input($data) {
      $data = trim($data);
      $data = stripslashes($data);
      $data = htmlspecialchars($data);

      return $data;
    }
  ?>

  <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
    <label for="name">Name : </label>
    <input id="name" type="text" name="name" value="<?php echo $name;?>"><span>* <?php echo $nameErr;?></span><br>
    <label for="email">E-Mail : </label>
    <input id="email" type="email" name="email" value="<?php echo $email;?>"><span>* <?php echo $nameErr;?></span><br>
    <span>* <?php echo $genderErr;?></span>
    <label>Gender : </label>
    <input type="radio" name="gender" <?php if (isset($gender) && $gender == "female") echo "checked"; ?> value="female"> Female
    <input type="radio" name="gender" <?php if (isset($gender) && $gender == "male") echo "checked"; ?> value="male"> Male
    <input type="submit" name="submit" value="Enter">
  </form>

  <?php
  echo "<h2>Your Input:</h2>";
  echo $name;
  echo "<br>";
  echo $email;
  echo "<br>";
  echo $gender;
  ?>

</body>

</html>

Look at this line: $name = test_input($name); . $name above is: $name = $gender = $email = "";

So you always pass empty string to test_input and always get empty string from it regardless of the contents of $_POST . It should be $name = test_input($_POST["name"]); . Same for email and gender fields: $email = test_input($email); -> $email = test_input($_POST['email']); and $gender = test_input($gender); -> $gender = test_input($_POST['gender']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM