表单不会使用锚点部分进行验证

时间:2014-05-26 09:48:20

标签: javascript php forms validation

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

要了解它应该如何运作,请查看独立的联系方式.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; 

&GT;

1 个答案:

答案 0 :(得分:1)

提交表单后,您的网址将为?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']);
}