I have a simple email contact form that and has multiple input fields. Righ开发者_StackOverflowt now, it only sends the value of one of the fields (message). How do I ammend the code so i can send all the info from the fields? I have about 15 fields.
Here is the code:
$('#submit').click(function(){
$.ajax({
type: "POST",
url: "mail.php",
data: ({email : $("#email").val(), message: $("#message").val()}),
cache: false,
error: function () {
alert('did not go thru');
},
success: function(html){
//$("#response").fadeIn("slow");
$("#tab1").html(html);
//setTimeout('$("#response").fadeOut("slow")',2000);
alert('mail sent');
}
});
});
If you give the form an id instead, and move the callback to the form's submit event, you can easily use the serialize method to automatically send the entire form:
$('#form-id').submit(function(){
$.ajax({
type: "POST",
url: "mail.php",
data: $(this).serialize(),
cache: false,
error: function () {
alert('did not go thru');
},
success: function(html){
//$("#response").fadeIn("slow");
$("#tab1").html(html);
//setTimeout('$("#response").fadeOut("slow")',2000);
alert('mail sent');
}
});
});
Why not adding the remaining fields to data?
精彩评论