php表单(2)

学习php表单 主要是想知道 前端通过submit之后 后端是如何进行操作的。现在实现一个效果:点击submit,输入框的信息不会被刷掉;刷新页面,输入框的信息被刷掉(index.php)。

<?php
$nameErr = $emailErr = $genderErr = $websiteErr = ‘‘;
$name = $email = $gender = $comment = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
   if(empty($_POST[‘name‘])){
       $nameErr = ‘name is required‘;
   }else{
       $name = $_POST[‘name‘];
   }

   if(empty($_POST[‘email‘])){
       $emailErr = "email is required";
   }else{
       $email = $_POST[‘email‘];
   }

   if(empty($_POST[‘website‘])){
       $website = "";
   }else{
       $website = $_POST[‘website‘];
   }

   if (empty($_POST["comment"])) {
    $comment = "";
  } else {
    $comment = $_POST["comment"];
  }

   if(empty($_POST[‘gender‘])){
       $genderErr = "gender is required";
   }else{
       $gender = $_POST[‘gender‘];
   }
}
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

    Name: <input type="text" name="name" value="<?php echo $name?>">
    <span class="error">* <?php echo $nameErr;?></span>
    <br><br>
    E-mail:
    <input type="text" name="email" value="<?php echo $email?>">
    <span class="error">* <?php echo $emailErr;?></span>
    <br><br>
    Website:
    <input type="text" name="website" value="<?php echo $website ?>">
    <span class="error"><?php echo $websiteErr;?></span>
    <br><br>
    <label>Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment ?></textarea>
    <br><br>
    Gender:
    <input type="radio" name="gender" value="female" <?php if(isset($gender) && $gender == ‘female‘) echo "checked"; ?>>Female
    <input type="radio" name="gender" value="male" <?php if(isset($gender) && $gender == ‘male‘) echo "checked"; ?>>Male
    <span class="error">* <?php echo $genderErr;?></span>
    <br><br>
    <input type="submit" name="submit" value="Submit"> 
 
    </form>
</body>
</html>

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。