Search code examples
phprequired-field

Required fields in php form not work properly


I would like to set all the fields of my form as "required". I know there are already similar questions with many good answers but unfortunately I could not find the problem with my code.

What I need is to show the error message ONLY if user has left some fields empty, but is if I answer all the questions in the form it shows the error "Oops...all fields are required". Could someone please help me?

Here is what I tried:

 session_start();

 if(isset($_POST['submit']))
  {
   $_SESSION['q1'] = $_POST['q1'];
   $_SESSION['q2'] = $_POST['q2'];
   $_SESSION['q3'] = $_POST['q3'];
   $_SESSION['q4'] = implode(',', $_POST['birthplace']);
   $_SESSION['q5'] = implode(',', $_POST['countrylived']);

   $q1 = mysql_real_escape_string($_SESSION['q1']);
   $q2 = mysql_real_escape_string($_SESSION['q2']);
   $q3 = mysql_real_escape_string($_SESSION['q3']);
   $q4 = mysql_real_escape_string($_SESSION['q4']);
   $q5 = mysql_real_escape_string($_SESSION['q5']);

   $required = array('q1', 'q2', 'q3', 'q4', 'q5');
   $error = false;
   foreach($required as $field) {
     if (empty($_POST[$field])) {
       $error = true;
     }
   }

   if ($error) {
      echo "Oooops!! It seems you did not answer all the questions. Please not that all fields are required.";
   } 
  else {

       $query="INSERT INTO test (q1, q2, q3, q4, q5) VALUES ('$q1', '$q2', '$q3', '" . $q4 . "', '" . $q5 . "')";
        mysql_query($query) or die (mysql_error() );
        header('Location: Thankyou.php'); 
        exit;
     }
}
    ?>

EDIT: here is part of my html form. (sorry it contains many questions so I just chose the part related to q1-5)

<ul id="page_1">

<div class="meter"><span style="width: 20%">Step 1</span></div>

<fieldset id = "Form1"> <h3> <legend>Please answer the following questions carefully in order to help us providing you with the best recommendation</legend> </h3>

<fieldset id = "q1"> <legend class="Q1"></legend>
<span> What is your gender?</span>
<div class="fieldset content">
<p>
<Input type = 'radio' Name ='q1' value = 'male'>Male
<Input type = 'radio' Name ='q1' value = 'female'>Female
</p>
</div>
</fieldset>


<fieldset id = "q2"> <legend class="Q2"></legend>
<span>  What is your age?</span>
<div class="fieldset content">
<p>
<Input type = 'radio' Name = 'q2' value = 'under 18'>Under 18
<Input type = 'radio' Name = 'q2' value = '18-30'>18-30
<Input type = 'radio' Name = 'q2' value = '31-40'>31-40
<Input type = 'radio' Name = 'q2' value = '41-60'>41-60
<Input type = 'radio' Name = 'q2' value = 'over 60'>Over 60
</p>
</div>
</fieldset>


<fieldset id = "q3"> <legend class="Q3"></legend>
<span> 3. What is your marital status?</span> 
<div class="fieldset content">
<p>
<Input type = 'radio' Name = 'q3' value = 'married'>Married or domestic partnership
<Input type = 'radio' Name = 'q3' value = 'single'>Single
<Input type = 'radio' Name = 'q3' value = 'would rather not say'>Would rather not say
<p>
</div>
</fieldset>

<fieldset id = "q4"> <legend class="Q4"></legend>
<span> 4. Where were you born?</span>
<div class="fieldset content">
<p>
<?php
mysql_connect("localhost", "user", "Mypass") or die("Connection Failed");
mysql_select_db("imdb")or die("Connection Failed");
$query = "SELECT * FROM Country"; 
$result = mysql_query($query);
?>

<select name="birthplace[]" multiple="multiple" width="200px" size="10px"> 

<?php while ($line = mysql_fetch_array($result)) { 
?>
<option value="<?php echo $line['country'];?>"> <?php echo $line['country'];?> 
 </option>  
 <?php 
 }  
 ?>
</select>  
</p>
</div>
</fieldset>


<fieldset id = "q5"> <legend class="Q5"></legend>
<span> 5. Please select the country/countries where you have lived there at least for 5 years?</span>
<div class="fieldset content">
// THE SAME PROCEDURE AS Q4 //

   ......
    .....
  <input type="submit" name="submit" value="Submit" class="button">
  </fieldset>
  </ul>

  </form>
  </div>
 </body>
 </html>

Solution

  • Your problem here is that your code is checking variables that will always be empty: note that it looks like you don't have $_POST['q4'] or $_POST['q5'] in your form.

    In your checker, try testing the session fields that you have assigned instead:

       $required = array('q1', 'q2', 'q3', 'q4', 'q5');
       $error = false;
       foreach($required as $field) {
         if (empty($_SESSION[$field])) {
           $error = true;
         }
       }
    

    Alternatively, you might want to check what is in your birthplace and countrylived fields. If they aren't arrays then the implode function won't return anything.

    If this doesn't work, add:

    if(empty($_SESSION['q1'])) {
        echo "q1 is empty!";
    }
    if(empty($_SESSION['q2'])) {
        echo "q2 is empty!";
    }
    if(empty($_SESSION['q3'])) {
        echo "q3 is empty!";
    }
    if(empty($_SESSION['q4'])) {
        echo "q4 is empty!";
    }
    if(empty($_SESSION['q5'])) {
        echo "q5 is empty!";
    }
    

    To find out which fields are the problem.