有没有一种简单的方法可以确保联系表格中的字段存在和/或有效(最好是存在)?

时间:2017-04-24 23:25:51

标签: php html5

我对php表单不是很好,需要帮助我正在处理的那个。首先,我想通过一种简单的方法来帮助确保所有字段都存在且有效。为了空间,我使用的是与此类似的表格 链接http://distancebrothers.com/request-quote.html

下面列出了php代码:

<?php

 .
 .
 .
$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 = "distancebrothers.com,jldouglas58@gmail.com";
$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: http://www.distancebrothers.com/quote-confirmation.html') 
;?>

1 个答案:

答案 0 :(得分:0)

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

您可能应该修剪变量或空格,以防用户试图绕过必填字段。修剪($值)