0

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?

  • how will you get a `$error` on your form page. you need to pass that variable again to the form page or echo it on the process.php – Meenesh Jain Jul 04 '15 at 14:52
  • Sample Please. Which one? –  Jul 04 '15 at 14:53
  • see my answer ... `$error` is a local variable so its scope is limited to process.php – Meenesh Jain Jul 04 '15 at 14:55
  • Thanks. Tried it but it did not work. Form still submits even on empty fields + the error variable wont work or display error. –  Jul 04 '15 at 15:02
  • if you want your form not to submit on empty fields you need JS for that . take a reference from here http://stackoverflow.com/questions/3937513/javascript-validation-for-empty-input-field – Meenesh Jain Jul 04 '15 at 15:07

2 Answers2

1

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'];
        } 
 ?>
Meenesh Jain
  • 2,532
  • 2
  • 19
  • 29
0

Either you need a form to redirect back to your form.php, or move the echo $Error to your process.php so you can show the error from that page.

Harpuia
  • 1
  • 4