我正在制作一个查询表格。我是jquery的新手。在阅读了jquery验证插件和ajax后,我编写了以下代码。此代码首先验证表单,然后通过ajax提交表单。如果我提交表格,表格提交两次。结果来了两次 我无法犯错误。
<script>
$(document).ready(function () {
$('#contactForm').validate({
rules: {
name: {
required: true
},
email: {
required: true,
email: true
},
address: {
required: true
},
message: {
required: true
},
security: {
required: true
}
},
messages: {
name: {
required: '* Please Enter Your Name.'
},
email: {
required: "* Please Enter Your E-mail ",
email: "* Please enter proper E-mail "
},
address: {
required: "* Please enter address."
},
message: {
required: "* Please enter Enquiry"
},
security: {
required: "* Please enter Security code"
}
},
submitHandler: function (form) {
// Ajax Submit here
// alert($("#contactForm").serialize());
$.ajax({
type: "POST",
url: "assets/sendmail.php",
data: $("#contactForm").serialize(),
success: function (result) {
if (result == 'Success') {
$("#contactForm").hide();
$("#contact_response").html('<div class="alert alert-success">Thank you for your feedback.</div>');
} else {
alert(result);
}
return false;
}
});
return false;
}
});
});
html表单是
<form action="assets/sendmail.php" method="post" name="contactForm" id="contactForm" >
<!-- STEP-2 : GENERAL SETTINGS -->
<input type="hidden" name="success_page" value="../contact-us?res=thank-you" id="success_page" /><!-- PAGE TO REDIRECT AFTER SUBMIT -->
<input type="hidden" name="email_to" value="<?=get_settings('contact_email');?>" id="email_to" /><!-- EMAIL ADDRESS WHERE WE WANT TO RECEIVE EMAIL -->
<input type="hidden" name="email_subject" value="Query Recieved" id="email_subject" /><!-- SUBJECT OF THE EMAIL -->
<!-- STEP-2 : GENERAL SETTINGS -->
<label for="name" class="nameLabel">Name</label>
<input id="name" type="text" name="name" />
<label class="control-label">Email address</label>
<div class="controls">
<input type="email" name="email" id="email" />
<p class="help-block"></p>
</div>
<label for="address">Address</label>
<input id="address" type="text" name="address" />
<label for="message" class="messageLabel">Enquiry</label>
<textarea id="message" name="message" /></textarea>
<label >Security Code</label>
<img src="classes/CaptchaSecurityImages.php?width=100&height=30&characters=5" alt="captcha" style="vertical-align:bottom;"/> <input id="security" name="security" class="required" type="text" style="width:30%;vertical-align:bottom;margin-bottom:0px;padding-bottom:0px;"/></br>
<button type="submit" id="submit" name="submit">Send</button>
</form>
</script>
答案 0 :(得分:1)
Your HTML & JavaScript code is working
但是,在您的OP中,您似乎已经在一组<script></script>
代码中包含了jQuery和HTML ...
您在JavaScript的开头有一个<script>
标记....
<script>
$(document).ready(function () {
$('#contactForm').validate({
rules: {
....
您的HTML结尾处有一个结束</script>
标记...
....
</form>
</script>
你不能这样做,它可能会打破整个页面。仅在<script></script>
代码中包含JavaScript ...
<script>
$(document).ready(function () {
$('#contactForm').validate({
// rules, options, etc.
});
});
</script>
您的action="assets/sendmail.php"
代码中也不需要<form>
。您已经.ajax()
使用jQuery Validation插件的assets/sendmail.php
将数据发送到submitHandler
。
我注意到的其他两个小问题......
1)您有一个结束</br>
标记。 This is invalid HTML because there is no such thing。 BR
is considered an "empty" or "void" tag所以你要么单独使用<br>
,要么“自我关闭”它:<br />
。 (注意斜杠的临界位置/
)
2)您的textarea
写得像这样:<textarea /></textarea>
。这是无效的HTML,因为当它已经有结束标记textarea
时,您无法“自我关闭”</textarea>
。你只需这样写:<textarea></textarea>
。
DEMO修正了HTML并运行jQuery验证: http://jsfiddle.net/a2xdg/
答案 1 :(得分:1)
只需在php代码中编写以下逻辑:
if(!empty($email)){
@mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
}
这样可以防止发送包含空值的电子邮件。