有人可以告诉我我的代码有什么问题。我无法弄清楚错误是什么。
联系表格
<form action="mail.php" method="POST">
<p>Name</p> <input type="text" name="name">
<p>Email</p> <input type="text" name="email">
<p>Phone</p> <input type="text" name="telephone">
<p>Request Phone Call:</p>
Yes:<input type="checkbox" value="Yes" name="call">
No:<input type="checkbox" value="No" name="call"><br />
<p>Priority</p>
<select name="priority" size="1">
<option value="Low">Low</option>
<option value="Normal">Normal</option>
<option value="High">High</option>
<option value="Emergency">Emergency</option>
</select>
<br />
<p>Type</p>
<select name="type" size="1">
<option value="update">Website Update</option>
<option value="change">Information Change</option>
<option value="addition">Information Addition</option>
<option value="new">General Enquiries</option>
</select>
<br />
<p>Message</p><textarea name="message" rows="10" cols="40"></textarea><br />
<input type="submit" value="Send" class="button"/><input type="reset" value="Clear" class="button"/>
</form>
mail.php
<?php
if(isset($_POST['email'])) {
$email_to = "myemail@sky.com";
$email_subject = "Tip Top Music";
function died($error) {
// Error codes
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();
}
// Required fields
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['message'])) {
died('Required Fields are not complete');
}
$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
if(!isset($_POST['call'])) {
$call = "No"; // if checkbox was left unchecked. Default is No
}
else {
$call = ($_POST['call'])
}
$priority = $_POST['priority']; // Will already have default value
$type = $_POST['type']; // Will already have default value
$message = $_POST['message']; // required
$error_message = "";
$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,$name)) {
$error_message .= 'The Name you entered does not appear to be valid.<br />';
}
if(strlen($message) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
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 .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Request Callback: ".($call)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Priority: ".($priority)."\n";
$email_message .= "Type: ".($type)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- Message success -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
我得到未定义的索引调用,指向$ call =“No”;线。然而,脚本根本不应该运行,因为我发布了一个空白表单?当我回显输入字段时它们是空白但是如果把IF语句放入检查它们是否被设置(isset)它进入语句就像它们已被设置一样?我应该使用除了isset之外的其他东西来检查空输入吗?这通常有效,所以我很困惑,为什么现在不呢?
答案 0 :(得分:3)
在$ _POST ['call']中添加缺少的分号后,您的代码运行正常;
正确的一个应该是$ call =($ _POST ['call']);
提交空表单时会显示错误消息。所以我认为验证工作正常。
答案 1 :(得分:1)
$_POST['call']
if(!isset($_POST['call'])) {
$call = "No"; // if checkbox was left unchecked. Default is No
}
else {
$call = $_POST['call']; // Missing semicolon here
}