PHP简单表单验证

时间:2015-07-04 14:48:23

标签: php css validation

我正在尝试创建一个简单的表单验证,并且表单不会提交,直到设置了所有字段。我这里有两个文件。 -form.php -process.php

出于某种原因,$error将不会显示,单选按钮将不会提交。有什么不对吗?

这是form.php:

    <?php
 if(isset($_GET['error']) &&  $_GET['error']!=""){
  echo  $_GET['error'];
 }
?>



    <body>
    <form action="process.php" method="POST">
       <p>
           <label for="name">Your Name:</label>
           <input type="text" name="name" id="name" value="">
       </p>

       <p>
           <label for="location">Dojo Location:</label>
           <select name="location">
              <option value="Mountain View">Mountain View</option>
              <option value="San Francisco">San Francisco</option>
              <option value="South Korea">South Korea</option>
              <option value="Philippines">Philippines</option>
            </select>
        </p>

        <p>
            <label for="language">Favorite Language:</label>
            <select name="language">
              <option value="JavaScript">JavaScript</option>
              <option value="PHP">PHP</option>
              <option value="Ruby">Ruby</option>
              <option value="Python">Python</option>
            </select>
        </p>

        <p>
            <label for="comment">Comment: (Optional)</label><br/>
            <textarea rows="10" cols="50" name="comment"></textarea>
        </p>

        <p>
             <label for="comment">Can we store cookies in your computer?</label>
             <input type="radio" name="cookies" value="yes">Yes
             <input type="radio" name="cookies" value="no">No
        </p>

          <input type="submit" value="Submit">
    </form>

这是process.php:

<?php 

        if (isset($_POST["submit"])) {
            if (empty($_POST["name"])) {
                $Error = "Missing Name";
            }

             if (empty($_POST["location"])) {
                $Error = "Missing Location";
            }

             if (empty($_POST["language"])) {
                $Error = "Missing language";
            } 

             if (empty($_POST["cookies"])) {
                $Error = "Select cookies";
            } 

        }else{

         $name = $_POST['name'];
         $location = $_POST['location'];
         $language = $_POST['language'];
         $comment = $_POST['comment'];
         $cookies = $_POST['cookies'];
    }

if($Error!=""){
   header("Location:form.php?error=".$Error);
 }

    ?>

    <h2>Submitted Information:</h2>
    <p><?php echo "NAME: {$name}"; ?> </p>
    <p><?php echo "DOJO LOCATION: {$location}"; ?></p>
    <p><?php echo "FAVORITE LANGUAGE: {$language}:"; ?> </p>
    <p><?php echo "COMMENT: {$comment}"; ?></p>
     <p><?php echo "COOKIES: {$cookies}"; ?></p>

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

process.php

中尝试类似的操作
 if($Error!=""){
   header("Location:form.php?error=".$Error);
 }

form.php

 if(isset($_GET['error']) &&  $_GET['error']!=""){
  echo  $_GET['error'];
 }

在您的process.php中将代码更改为

 <?php 
        if (isset($_POST["submit"])) {
            $Error ="";
            if (isset($_POST["name"]) &&  $_POST["name"]!="") {
                $Error = "Missing Name";
            }

             if (isset($_POST["location"]) &&  $_POST["location"]!="") {
                $Error = "Missing Location";
            }

             if (isset($_POST["language"]) &&  $_POST["language"]!="") {
                $Error = "Missing language";
            } 

             if (isset($_POST["cookies"]) &&  $_POST["cookies"]!="") {
                $Error = "Select cookies";
            }  

            if($Error!=""){
               header("Location:form.php?error=".$Error);
             }

             $name = $_POST['name'];
             $location = $_POST['location'];
             $language = $_POST['language'];
             $comment = $_POST['comment'];
             $cookies = $_POST['cookies'];
        } 
 ?>

答案 1 :(得分:0)

您需要将表单重定向回form.php,或将echo $Error移至您的process.php,以便显示该页面中的错误。