学习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>
时间: 2024-10-10 21:19:14