Form won't validate with an anchor section
Form won't validate with an anchor section
我在一个独立的联系人表单上有一个表单,但是当我试图在滚动页面的底部或带有锚点的部分中使用它时,它将无法在视觉上验证。换句话说,错误消息不会出现。如果填写了所有必需的字段,表单将发送一封电子邮件。但是视觉上什么都没有发生!?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']);
}
相关文章:
- Fighting with FRP
- issue with FB.Event.subscribe
- geolocation-marker.js conflict with markerclusterer.js
- Angular 2.0 with JavaScript or TypeScript?
- 为什么不推荐使用“with”?是否有更好或其他方法可以“下降”到对象的命名空间
- WebComponentsJS with IE10
- Javascript setTimeout for an array
- 指令的模板必须只有一个根元素:With restrict E&替换true
- Animating text along an circular path with svg & javascr
- Dissect an id with javascript?
- HREF within an IFrame with JQuery
- jQuery AJAX with an interval
- Change an image with "onmouseover"
- Get an specific value in <a> html with javascript-ajax
- jQuery .find() with an iD
- Animating an svg with css
- Form won't validate with an anchor section
- Changing css of an html element with 'Uncaught TypeError
- getElementById with an array Javascript
- Having an issue with javascript Date()