Form won't validate with an anchor section

Form won't validate with an anchor section

本文关键字:with an anchor section validate won Form      更新时间:2023-09-26

我在一个独立的联系人表单上有一个表单,但是当我试图在滚动页面的底部或带有锚点的部分中使用它时,它将无法在视觉上验证。换句话说,错误消息不会出现。如果填写了所有必需的字段,表单将发送一封电子邮件。但是视觉上什么都没有发生!?http://patrickmchugh.com/test/

要查看它应该如何工作,请参阅独立的contact.php patrickmchugh.com/test/contact.php这是错误消息应该如何工作的

<?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#content'); 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; 

?>

提交表单后,您的url将是?e=You+must+enter+your+name.#content2您需要在表单页面上获取和回显错误,如:-

if(isset($_GET['e'])) {
  echo $_GET['e'];  // or echo urldecode($_GET['e']);
}

与成功提交后相同,如:-

if(isset($_GET['s'])) {
  echo $_GET['s'];  // or echo urldecode($_GET['s']);
}