I have multiple forms on one web page. I need that user could post data from any form and this data must send to my email. Now working only 1st form.
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 = '[email protected]';
$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('');
});
What should I change? How can I submit all 3 forms?
You can use single form like:
<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('');});
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments