【发布时间】:2014-05-13 13:27:49
【问题描述】:
您好,请原谅菜鸟的问题,因为我还在学习 PHP。我正在尝试进行基本的表单验证。我试图让验证时发现的任何错误在表单字段内以红色显示。如果没有发现错误,表单应该重定向到感谢页面。
我已经写了一个基本的验证,但我现在被困在如何正确地实现它上。
我的问题是:
- 如何在不发送表单的情况下让错误消息显示在表单字段中?
- 如果没有发现错误,我该如何发送表单?
我的代码如下:
HTML
<form id="enquire" method="post" action="thankyou.php">
<h2>Test Drive an Audi Today</h2>
<input type="text" value="Name" class="textbox" id="name" name="name" onfocus="if(this.value=='Name') this.value='';" /><br />
<br />
<input type="text" value="Surname" class="textbox" id="surname" name="surname" onfocus="if(this.value=='Surname') this.value='';" /><br />
<br />
<input type="text" value="Contact Number" class="textbox" id="nr" name="nr" onfocus="if(this.value=='Contact Number') this.value='';" /> <br />
<br />
<input type="text" value="Email" class="textbox" id="email" name="email"onfocus="if(this.value=='Email') this.value='';" /><br />
<br />
<select id="swapImg" name="model" class="modelSelect" onchange="swapImage()" >
<option value="images/1.jpg">A4</option>
<option value="images/2.jpg" selected="selected">A6</option>
<option value="images/3.jpg">A8</option>
</select>
<br />
<br />
<input type="submit" name="submit" class="butt" value="Send" />
<span class="buttonText">We'll Call you Back!</span>
</form>
PHP
if (isset($_POST['enquire'])){
//condition
$correct = false;
//Get Info
$name = $_POST['name'];
$surname = $_POST['surname'];
$number = $_POST['nr'];
$email = $_POST['email'];
$_REQUEST['model'];
//name fields
$string_exp = "/^[A-Za-z .'-]+$/";
//email field
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
//validate name field
if($name == ""){
$name_error="* Please enter your name";
$correct = false;
}
else if (!preg_match($string_exp,$name)) {
$name_error= "* Wrong Format";
$correct= false;
}
//validate surname
if($surname == ""){
$surname_error= "* Please enter your surname.";
$correct= false;
}
else if(!preg_match($string_exp,$surname)){
$surname_error= "* Wrong Format";
$correct = false;
}
//check email
if($email==""){
$email_error="* Please enter your email";
$correct = false;
}
else if(!preg_match($email_exp,$email)){
$email_error = "* Wrong Format";
$correct = false;
}
//check number
if($number=""){
$phone_error = "* Please enter your phone number";
$correct = false;
}
else if(!is_numeric($number)){
$phone_error ='* Please enter only numbers 0-9.';
$correct = false;
}
//proceed if all fields validate
if($correct){
$email_to = "tim@yah.co";
$subject ="New Enquiry";
//set message
$email_message .= "First Name: ".($name)."\n";
$email_message .= "Last Name: ".($lastname)."\n";
$email_message .= "Email: ".($email)."\n";
$email_message .= "Telephone: ".($telephone)."\n";
$email_message .= "Model: ".($model)."\n";
// create email headers
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
@mail($email, $email_subject, $email_message, $headers);
header("thankyou.php");
exit();
}//end if
}// end isset
如果有人能指出我正确的方向,将不胜感激。
【问题讨论】:
-
如果您想在不发送表单的情况下验证表单,则需要使用 javascript。而且你必须在 PHP 中重新检查表单。
-
记住... PHP 在服务器而不是客户端上运行,因此您必须使用 javascript。
标签: php forms validation