Reputation: 459
I need to post button value with serialize() function. problem is serialize() will not post button values. I have in my form SAVE, UPDATE, and DELETE buttons.Then PHP must know which button click to particular operation. I can get button value with click function , but how it to pass to the PHP page with Ajax. My codes are below ,also get from stackoverflow.
$(document).ready(function()
{
$("#btnUpdate").click(function() {
alert($(this).attr("value"));
/* attach a submit handler to the form */
$("#form1").submit(function(event) {
/* stop form from submitting normally */
event.preventDefault();
/*clear result div*/
$("#result").html('');
/* get some values from elements on the page: */
var values = $(this).serialize();
/* Send the data using post and put the results in a div */
$.ajax({
url: "form.php",
type: "post",
data: values,
success: function(data){
alert("success");
alert(data);
$("#result").html('submitted successfully');
},
error:function(){
alert("failure");
$("#result").html('there is error while submit');
}
});
});
});
});
Upvotes: 1
Views: 2159
Reputation: 859
i think you would like to try this
$(document).ready(function()
{
$("#btnUpdate").click(function() {
/* attach a submit handler to the form */
$("#form1").submit(function(event) {
/* stop form from submitting normally */
event.preventDefault();
/*clear result div*/
$("#result").html('');
/* get some values from elements on the page: */
var values = $(this).serialize();
/* Send the data using post and put the results in a div */
$.ajax({
url: "form.php",
type: "post",
data: values + '&' + 'button=' $(this).attr('value'),
success: function(data){
alert("success");
alert(data);
$("#result").html('submitted successfully');
},
error:function(){
alert("failure");
$("#result").html('there is error while submit');
}
});
});
});
});
Upvotes: 0
Reputation: 3181
Use this to get button value, put it right where you have the alert:
var buttonvalue = $(this).val();
and then you can append it to the serialized data by doing this:
var values = $('#form1').serialize();
values = values + '&button='+buttonvalue;
The '&button=' is what it will be called when you are testing to see which button was used.
Upvotes: 3
Reputation: 16623
The documentation states that No submit button value is serialized since the form was not submitted using a button.
.
Anyway, you could do it by appending the button name and value to serialize()
output like in this answer.
Upvotes: 0