在这里,我想验证我的下拉框,以便如果选择了...,则会出错。我已经这样做但是当提交表单时,错误没有被捕获,它只是提交。 ##只是为了澄清我已经全力以赴地投入0而它仍然没有工作。##
这是我的HTML
<html>
<head>
<title>Submit Form</title>
</head>
<body>
<p>All fields marked with an asterisk "<span style="color: red">*</span>"are mandatory.</p>
<form name="SubmitForm" method="post" action="SubmitForm.php">
<table>
<tr>
<td valign="top" width=250px>
<label for="first_name"><span style="font-weight:bold">First Name <span style="color: red">*</span></label>
</td>
<td valign="top">
<input type="text" name="first_name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top"">
<label for="last_name"><span style="font-weight:bold">Last Name <span style="color: red">*</span></label>
</td>
<td valign="top">
<input type="text" name="last_name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="email"><span style="font-weight:bold">Email Address <span style="color: red">*</span></label>
</td>
<td valign="top">
<input type="text" name="email" maxlength="80" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="telephone"><span style="font-weight:bold">Telephone Number <span style="color: red">*</span></label>
</td>
<td valign="top">
<input type="text" name="telephone" maxlength="30" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="formFood"><span style="font-weight:bold"> What is your favourite food?<span style="color: red">*</span>
</td>
<td valign="top">
<select name="formFood" style="display:inline">
<option value="0">Select...</option>
<option value="L">Lasagne</option>
<option value="F">Fish and Chips</option>
<option value="T">Toad in the Hole</option>
<option value="S">Steak and Chips</option>
</select>
</td>
</tr>
<tr>
<td height="30">
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center">
<input type="submit" value="Submit"> <a href="http://localhost/var/www/pages /SubmitForm.php"> </a>
</td>
</tr>
</table>
</form>
</body>
</html>
这是我的PHP
<?php
if(isset($_POST['email'])) {
//Mail to this email
$email_to = "submitform2@gmail.com";
$email_subject = "Submit form";
function died($error) {
// Begin error code
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['formFood'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // required
$formFood = $_POST['formFood']; // required
$error_message = "Please Enter correct values into all mandatory Feilds";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(isset($_REQUEST['formFood']) && $_REQUEST['formFood'] == '0') {
echo 'Please select a Food.';
}
if(strlen($telephone) > 11) {
died($error_message); }
if(strlen($telephone) < 11 ) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
mail("submitform2@gmail.com", $email_subject,
$email_message, "From:" . $email);
echo "Thank you for using our mail form";
}
else
//if "email" is not filled out, display the form
{
echo "<form method='post' action='mailform.php'>
Email: <input name='email' type='text'><br>
Subject: <input name='subject' type='text'><br>
Message:<br>
<textarea name='message' rows='15' cols='40'>
</textarea><br>
<input type='submit'>
</form>";
}
?>
<script>
alert("Thank you for contacting us. We will be in touch with you very soon.")
</script>
<?php
}
?>
答案 0 :(得分:0)
您没有检查选项的值,而是选项的文本:
if(isset($_REQUEST['formFood']) && $_REQUEST['formFood'] == 'Select...') {
echo 'Please select a Food.';
}
必须是:
if(isset($_REQUEST['formFood']) && $_REQUEST['formFood'] == '0') {
echo 'Please select a Food.';
}
答案 1 :(得分:0)
不使用0
,而是使用您不希望有效的<option>
空值,即<option value="">Select...</option>
。一般来说这只是一个很好的做法...如果在某些时候你决定包括前端验证,这种配置也可以很好地开箱即用。
检查$ _POST变量时,请改用:
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
(!isset($_POST['formFood'] || empty($_POST['formFood']))) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
答案 2 :(得分:0)
应该是
if(isset($_REQUEST['formFood']) && $_REQUEST['formFood'] == 0) {
echo 'Please select a Food.';
}
答案 3 :(得分:0)
您正在检查提交的select值是否为'Food ...',但您应该检查的值是'0'(在选择框的值中设置的数字,而不是标题。
见下文:
if(isset($_REQUEST['formFood']) && $_REQUEST['formFood'] == '0') {
echo 'Please select a Food.';
答案 4 :(得分:0)
使用if(空($ _POST ['formFood']))如果值为= 0则返回false
Returns FALSE if var exists and has a non-empty, non-zero value. Otherwise returns TRUE.
The following things are considered to be empty:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)