码迷,mamicode.com
首页 > Web开发 > 详细

php表单(2)

时间:2015-05-11 19:41:38      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

学习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>

 

php表单(2)

标签:

原文地址:http://www.cnblogs.com/wang-jiang/p/4495082.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!