gpt4 book ai didi

php - 提交联系表单并使用 validate.js

转载 作者:行者123 更新时间:2023-11-28 18:53:56 25 4
gpt4 key购买 nike

这是 getting validate.js to work 的推导和 How to make my contact form send me Email

我正在使用 validate.js 和 php 构建一个简单的联系表单。我已经完成了验证,并开始使用 php 调用服务器以获取功能 - 但是,现在不是提交表单而是生成 JS 验证确认。我如何允许它提交?结合验证确认并正确提交它会很酷(例如,'太棒了,你填写了表格,它是通过带有验证的 JS 设置提交的)但是让它提交并重定向到确认页面也很好。下面是代码:

更新于美国东部时间 12 月 15 日下午 4:17在头部调用库:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>

JS 被内联调用:

  <script type="text/javascript">
SubmittingForm=function() {
document.myform.submit();

    $(document).ready(function() {
$("#fvujq-form1").validate({
submitHandler:function(form) {
SubmittingForm();
},
rules: {
name: "required", // simple rule, converted to {required:true}
email: { // compound rule
required: true,
email: true
},
url: {
url: true
},
comment: {
required: true
}
},
messages: {
comment: "Enter your damn comment."
}
});
});

jQuery.validator.addMethod(
"selectNone",
function(value, element) {
if (element.value == "none")
{
return false;
}
else return true;
},
"Please select an option."
);

$(document).ready(function() {
$("#fvujq-form2").validate({
submitHandler:function(form) {
SubmittingForm();
},
rules: {
sport: {
selectNone: true
}
}
});
});
</script>

表单标记:

 <p style="font-style:italic; font-size:12px; font-weigh: normal; margin-top: -89px;     margin-left: 33px;">Contact me written in a different language.</p> <img src="http://www.cameroncashwell.com/imgs/pointing-left.png" style="float: right; margin-right: 140px; margin-top: -89px;">

<div class="form-div">
<form id="fvujq-form1" style="font-size:22px; color:#333;" method="post" action="send_form_email.php">
<div class="form-row"><span class="label">Name *</span><input type="text" name="name" /></div>
<div class="form-row"><span class="label">Email *</span><input type="text" name="email" /></div>
<div class="form-row"><span class="label">URL</span><input type="text" name="url" /></div>
<div class="form-row"><span class="label">Comment *</span><textarea name="comment"></textarea></div>
<div class="form-row"><input class="submit" type="submit" value="Submit"></div>
</form>
</div>

在“send_form_email.php”中:

<?php
if(isset($_POST['email'])) {

// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "chaseoutt@gmail.com";
$email_subject = "How to be a cool";


function died($error) {
// your error code can go here
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();
}

// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['url']) ||
!isset($_POST['project']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}

$name = $_POST['name']; // required
$email = $_POST['email']; // required
$url = $_POST['url']; // required
$project = $_POST['project']; // not required
$comments = $_POST['comments']; // 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,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 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)."\n";
$email_message .= "URL: ".clean_string($url)."\n";
$email_message .= "Project: ".clean_string($project)."\n";
$email_message .= "Comments: ".clean_string($comments)."\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);
?>

<!-- include your own success html here -->

Thank you for contacting us. We will be in touch with you very soon.

<?php
}
?>

现在,当我正确填写表单时,我从 php 文件 (action="send_form_email.php") 中声明了这些错误:

We are very sorry, but there were error(s) found with the form you submitted. These errors appear below.

We are sorry, but there appears to be a problem with the form you submitted.

Please go back and fix these errors.

最佳答案

在你的 php 检查中你有 !isset($_POST['comments']))但在你的 html 中你有 <textarea name="comment">你需要使它们相同,<textarea name="comments">更改它,只要您正确填写所有字段,您就应该准备就绪。

关于php - 提交联系表单并使用 validate.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8525955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com