php - Is there a simple way to ensure that fields within a contact form are present and/or valid (preferably present)?


I am a not very good with php forms and need help with the one I am working on. First and foremost I would like help with a simple way to ensure that all fields are present and valid. For the sake of space, I am using a form similar to this link

The php code is listed below:


$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$cmpny = $_POST['cmpny'];
$street = $_POST['street'];
$city_st_zip = $_POST['city_st_zip'];
$respond = $_POST['respond'];
$num_passengers = $_POST['num_passengers']; 
$trip_type = $_POST['trip_type'];   
$num_pickups = $_POST['num_pickups'];   
$bus_size = $_POST['bus_size'];
$pickup_name = $_POST['pickup_name'];       
$pickup_street_name = $_POST['pickup_street_name']; 
$pickup_city_st_zip = $_POST['pickup_city_st_zip'];
$depart_date = $_POST['depart_date'];
$depart_time = $_POST['depart_time'];
$dropoff_name = $_POST['dropoff_name'];
$dropoff_street_name = $_POST['dropoff_street_name'];
$dropoff_city_st_zip = $_POST['dropoff_city_st_zip'];
$dropoff_date = $_POST['dropoff_date']; 
$dropoff_time = $_POST['dropoff_time'];
$spec_instr = $_POST['spec_instr'];
$meet_greet = $_POST['meet_greet'];
$ada = $_POST['ada'];
$extra = $_POST['extra'];
$find_us = $_POST['find_us'];
$formcontent="<html> <body><b>Contact Information:</b>  <br><br>Name: $name 
<br>Email: $email <br>Phone: $phone <br>Company / Group Name: $cmpny 
<br>Street Name: $street <br>City, State, Zip Code: $city_st_zip <br>Respond 
Via:  $respond <br><br><b>Event / Trip Details:</b> <br><br>Number of 
Passengers:  $num_passengers <br>Trip Type:  $trip_type <br>Number of 
Pickups:  $num_pickups <br>Bus Size:  $bus_size <br><br><b>Event / Trip 
Times - Departure and Destination:<br>Pick up location</b>  <br><br>Name:  
$pickup_name <br>Street Name:  $pickup_street_name <br>City, State, Zip 
Code:  $pickup_city_st_zip <br>Depart Date:  $depart_date <br>Depart Time:  
$depart_time <br><br><b>Drop Off Location</b><br><br>Name:  $dropoff_name 
<br>Street Name:  $dropoff_street_name <br>City, State, Zip Code:  
$dropoff_city_st_zip <br>Return Date:  $dropoff_date <br>Return Time:  
$dropoff_time <br><br><b>Contact Information:</b>  <br><br>Itinerary / 
Special Instructions / Additional Info:<br>$spec_instr <br>Airport Meet & 
Greet:  $meet_greet <br>ADA Accessible Coach:  $ada <br>Extra Storage:  
$extra <br>How did you find us?  $find_us </body> </html>";
$recipient = ",[email protected]";
$subject = "Request for a Quote";
$mailheader = "From: $email \r\n";
$mailheader .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header( 'Location:') 



$valid = 1;    
foreach($_POST as $key=>$value)
   echo "$key=$value";
     if ($value=="" || $value===null)
           { $valid = 0; }

You should probably trim the variables or white spaces, just in case a user tries to get around a required field. trim($value)

People are also looking for solutions to the problem: php - Retrive data from database to java


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.