Forcing Users to Fill in All Fields in a Form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pura_vida
Forum Newbie
Posts: 6
Joined: Mon Sep 26, 2005 11:05 am
Location: Chicago, IL

Forcing Users to Fill in All Fields in a Form

Post 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!
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post 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()...
Post Reply