我在一个网页上有多个表单。我需要用户可以从任何表单发布数据,这些数据必须发送到我的电子邮箱。现在只处理第一种形式。
HTML:
<form name="sentMessage" id="contactForm" novalidate>
<div>
<label>Name</label>
<input type="text" placeholder="Name" class="name">
</div>
<div>
<label>Email</label>
<input type="email" placeholder="Email" class="email">
</div>
<div id="success"></div>
<button type="submit">Submit</button>
</form>
<hr>
<form name="sentMessage" id="contactForm" novalidate>
<div>
<label>Name</label>
<input type="text" placeholder="Name" class="name">
</div>
<div>
<label>Email</label>
<input type="email" placeholder="Email" class="email">
</div>
<div id="success"></div>
<button type="submit">Submit</button>
</form>
<hr>
<form name="sentMessage" id="contactForm" novalidate>
<div>
<label>Name</label>
<input type="text" placeholder="Name" class="name">
</div>
<div>
<label>Email</label>
<input type="email" placeholder="Email" class="email">
</div>
<div id="success"></div>
<button type="submit">Submit</button>
</form>
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
// Create the email and send the message
$to = 'my@email.com';
$email_subject = "Email from $name";
$email_body = " Details:\n\n"."
Name: $name\n\n
Email: $email\n\n";
$headers = "Content-type: text/plain; charset=utf-8";
mail($to,$email_subject,$email_body,$headers);
return true;
?>
JS:
$(function() {
$("input").jqBootstrapValidation({
preventSubmit: true,
submitError: function($form, event, errors) {
// additional error messages or events
},
submitSuccess: function($form, event) {
event.preventDefault(); // prevent default submit behaviour
// get values from FORM
var name = $("input.name").val();
var email = $("input.email").val();
var firstName = name; // For Success/Failure Message
// Check for white space in name for Success/Fail message
if (firstName.indexOf(' ') >= 0) {
firstName = name.split(' ').slice(0, -1).join(' ');
}
$.ajax({
url: "././mail/contact.php",
type: "POST",
data: {
name: name,
email: email
},
cache: false,
success: function() {
// Success message
$('#success').html("<div class='alert alert-success'>");
$('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
.append("</button>");
$('#success > .alert-success')
.append("<strong>Your message has been sent. </strong>");
$('#success > .alert-success')
.append('</div>');
//clear all fields
$('#contactForm').trigger("reset");
},
error: function() {
// Fail message
$('#success').html("<div class='alert alert-danger'>");
$('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
.append("</button>");
$('#success > .alert-danger').append("<strong>Sorry " + firstName + ", it seems that my mail server is not responding. Please try again later!");
$('#success > .alert-danger').append('</div>');
//clear all fields
$('#contactForm').trigger("reset");
},
})
},
filter: function() {
return $(this).is(":visible");
},
});
$("a[data-toggle=\"tab\"]").click(function(e) {
e.preventDefault();
$(this).tab("show");
});
});
/*When clicking on Full hide fail/success boxes */
$('#name').focus(function() {
$('#success').html('');
});
我应该改变什么?我如何提交所有3份表格?
答案 0 :(得分:0)
将所有按钮类型从submit
更改为button
,并将onClick事件添加到呼叫功能,即
<input type "button" onclick="savedata()" />
,功能如下:
<script>
function savedata()
{
.... your ajax request here....
}
</script>
答案 1 :(得分:0)
您可以使用以下单一表单:
<form name="sentMessage" id="contactForm" novalidate>
<div>
<label>Name</label>
<input type="text" name="name[]" placeholder="Name" class="name">
</div>
<div>
<label>Email</label>
<input type="email" name="email[]" placeholder="Email" class="email">
</div>
<div>
<label>Name</label>
<input type="text" name="name[]" placeholder="Name" class="name">
</div>
<div>
<label>Email</label>
<input type="email" name="email[]" placeholder="Email" class="email">
</div>
<div id="success"></div>
<button type="submit">Submit</button>
</form>
JS
$(function() {
$("input").jqBootstrapValidation({
preventSubmit: true,
submitError: function($form, event, errors) {
// additional error messages or events
},
submitSuccess: function($form, event) {
event.preventDefault(); // prevent default submit behaviour
// get values from FORM
var name = $("input.name").val();
var email = $("input.email").val();
var firstName = name; // For Success/Failure Message
// Check for white space in name for Success/Fail message
if (firstName.indexOf(' ') >= 0) {
firstName = name.split(' ').slice(0, -1).join(' ');
}
$.ajax({
url: "././mail/contact.php",
type: "POST",
data: $('#contactForm').serialize(),
cache: false,
success: function() {
// Success message
$('#success').html("<div class='alert alert-success'>");
$('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
.append("</button>");
$('#success > .alert-success')
.append("<strong>Your message has been sent. </strong>");
$('#success > .alert-success')
.append('</div>');
//clear all fields
$('#contactForm').trigger("reset");
},
error: function() {
// Fail message
$('#success').html("<div class='alert alert-danger'>");
$('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
.append("</button>");
$('#success > .alert-danger').append("<strong>Sorry " + firstName + ", it seems that my mail server is not responding. Please try again later!");
$('#success > .alert-danger').append('</div>');
//clear all fields
$('#contactForm').trigger("reset");
},
})
},
filter: function() {
return $(this).is(":visible");
},
});
$("a[data-toggle=\"tab\"]").click(function(e) {
e.preventDefault();
$(this).tab("show");
});});
/*When clicking on Full hide fail/success boxes */
$('#name').focus(function() {
$('#success').html('');});