简体   繁体   中英

PHP Simple Form Validation

I am trying to create a simple form validation and the form wont submit until all of the fields were set. I have two files here. -form.php -process.php

For some reason the $error won't appear and the radio button wont submit. Is there anything wrong?

here's the 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>

here's the 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>

Any idea?

Try something like this in your process.php

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

On your form.php

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

In your process.php change the code to

 <?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'];
        } 
 ?>

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

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