在这里,我想验证我的下拉框,以便如果选择了 select... 则会出错。我已经这样做了,但是当表单提交时,错误没有被捕获,它只是提交。##只是为了澄清我已经厌倦了放置 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
}
?>