PHP表单验证 - 没有看到错误消息

时间:2014-09-07 21:33:07

标签: php validation

这是form

当我单击提交而未输入任何所需数据时,我没有看到错误消息,它只是跳转到页面顶部。我怎样才能解决这个问题?

添加到表单头部的PHP验证代码是:

if ($_POST['submit'])
{

/* Check all form inputs and strip unnecessary characters from data */

$quotetype = $_POST['quotetype'];
$name = check_input($_POST['name']);
$phone = check_input($_POST['phone']);
$email = check_input($_POST['email']);
$zipcode = check_input($_POST['zipcode']);
$appgender = $_POST['appgender'];
$appdob = check_input($_POST['appdob']);
$appsmoker = $_POST['appsmoker'];
$spousedob = check_input($_POST['spousedob']);
$spousesmoker = $_POST['spousesmoker'];
$child1gender = check_input($_POST['child1gender']);
$child1dob = check_input($_POST['child1dob']);
$child2gender = check_input($_POST['child2gender']);
$child2dob = check_input($_POST['child2dob']);
$child3gender = check_input($_POST['child3gender']);
$child3dob = check_input($_POST['child3dob']);
$child4gender = check_input($_POST['child4gender']);
$child4dob = check_input($_POST['child4dob']);
$currentcarrier = check_input($_POST['currentcarrier']);
$carriertype = $_POST['carriertype'];
$coverage = $_POST['coverage'];
$deductible = check_input($_POST['deductible']);
$premium = check_input($_POST['premium']);
$officecopay = check_input($_POST['officecopay']);
$rxcopay = check_input($_POST['rxcopay']);
$medconditions = $_POST['medconditions'];
$coverageamount = $_POST['coverageamount'];
$comments = $_POST['comments']; 

if (is_array($quotetype)) 
$displayquotetype = implode(",",$quotetype);

$errorstring = ""; //default value of error string

/* Check for required fields */

if (empty($quotetype))
$errorstring = $errorstring."*Quote Type "; 
if (!$name)
$errorstring = $errorstring."*Name ";
if (!$phone)
$errorstring = $errorstring."*Phone ";
if (!$email)
$errorstring = $errorstring."*Email ";
if (!$zipcode)
$errorstring = $errorstring."*Zip Code ";   
if ($appgender=="choose")
$errorstring = $errorstring."*Gender ";
if ($appsmoker=="choose")
$errorstring = $errorstring."*Smoker";

if ($errorstring =="")  {       
$to = 'name@domain.com';

$subject = 'Quote Request';         
$message = "    <html><body> </body></html>     ";   

$headers = "MIME-Version: 1.0" . "\r\n";    
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; 

mail($to,$subject,$message,$headers);

header('location: messagesent.html');
exit();
} 

}       

/* Functions used */

function check_input($data)     
{       
$data = trim($data);        
$data = stripslashes($data);    
$data = htmlspecialchars($data);        
return $data;   
 } 

index.php中的表单:

<form id="quote-form" name="quote-form" method="post" action="index.php">
...
<?php
if ($errorstring!="")
{echo "<span class='errormessage'>Please fill out the following fields: $errorstring<br></span>";}
?>
...
</form>

1 个答案:

答案 0 :(得分:0)

添加&#34;名称&#34;属性为您的提交输入。

<input type="submit" class="rounded-button button-shadow" id="submit" value="Submit" name="submit" />