Page 1 of 1

Forcing Users to Fill in All Fields in a Form

Posted: Wed Dec 21, 2005 11:54 am
by pura_vida
If a user leaves a field blank in my php form, they are getting an error message after the click SUBMIT.

What do I need to add to my code so that they are forced to fill in all fields?

Here's my current code:




Code: Select all

<?php
$avail = ''; 
foreach ($availability as $val) 
  $avail .= $val.", "; 
$tech = ''; foreach ($techSkills as $val) 
  $tech .= $val.", "; 
$act = ''; 
foreach ($activities as $val) 
  $act .= $val.", "; 

$formsent = mail("email@email.org", "Volunteer Form: $name", "Name: $name\r\n\r\nJob email: $email\r\n\r\nPhone: $phone\r\n\r\nAdress: $address\r\n\r\ncity: $city\r\n\r\nstate: $state\r\n\r\nZipCode: $zipCode\r\n\r\nbirthdate: $birthmonth $birthdate $birthyear\r\n\r\nCollege or Certificates: $college\r\n\r\nnative Tongue: $language\r\n\r\nOther Languages: $addlLanguages\r\n\r\nTimes Available: $avail\r\n\r\nHow Often: $howOften\r\n\r\nSpecialities: $skills\r\n\r\nSkills: $tech\r\n\r\nExperience: $youngLearners\r\n\r\nWilling to Participate in: $act\r\n\r\nHow Did You Hear ABout Us?: $hearAbout\r\n\r\nDo you have a car? $car\r\n\r\nType of Agent: $agent");

header("Location:http://www.website.com");
?>

THANKS FOR YOUR HELP!

Posted: Wed Dec 21, 2005 12:49 pm
by josh
forced to fill them in and the form won't get submited until then? You can't do this but you can use javascript to try and prevent them. You will always need to check the form data after submit just incase.

Posted: Wed Dec 21, 2005 1:56 pm
by jayshields
I don't think he means that.

I think what you need to do is use something like:

Code: Select all

if(!isset($_POST['field'])) {
  echo 'You haven\'t entered anything in field! Try again!';
} else {
  $completedfield = mysql_real_escape_string($_POST['field']); //Make the info safe (stripslashes or w/e could be used here instead)
  //...send $completedfield to db/file or whatever here...
}
or you could use empty() instead of isset()...