表单有效,它实际验证,如果未填写必填字段,则不会发送电子邮件,但是它无法直观地告诉观看者。如果代码用于独立的联系页面它工作正常。但是当合并到具有锚链接的单页站点时,它不会显示错误消息,而是重新加载到页面顶部。让观众没有错误消息。 任何想法请参阅http://patrickmchugh.com/test/
<?php
// check for form submission - if it doesn't exist then send back to contact form
if (!isset($_POST['save']) || $_POST['save'] != 'contact') {
header('Location: index.php#content2'); exit;
}
// get the posted data
$name = $_POST['contact_name'];
$email_address = $_POST['contact_email'];
$subject = $_POST['contact_subject'];
$message = $_POST['contact_message'];
// check that a name was entered
if (empty($name))
$error = 'You must enter your name.';
// check that an email address was entered
elseif (empty($email_address))
$error = 'You must enter your email address.';
// check for a valid email address
elseif (!preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', $email_address))
$error = 'You must enter a valid email address.';
// check that a message was entered
elseif (empty($message))
$error = 'You must enter a message.';
// check if an error was found - if there was, send the user back to the form
if (isset($error)) {
header('Location: index.php?e='.urlencode($error).'#content2'); exit;
}
// write the email content
$email_content = "Name: $name\n";
$email_content .= "Email Address: $email_address\n";
$email_content .= "Subject: $subject\n";
$email_content .= "Message:\n\n$message";
// send the email
mail ("patrick@patrickmchugh.com", "Enquiry from Connolly O'Neill Website", $email_content);
// send the user back to the form
header('Location: index.php?s='.urlencode('Thank you for your message.').'#contact2'); exit;
&GT;
答案 0 :(得分:1)
你必须把页面参数#contanet2或其他任何#parameter结尾的url。 你的其他参数是在...之前..
标题(&#39;位置:index.php?s =&#39; .urlencode(&#39;感谢您的留言。&#39;)。&#39; #contact2&#39;) ;出口;
标题(&#39;位置:index.php?e =&#39; .urlencode($ error)。&#39; #content2&#39;);出口;