serialize() 関数を使用してボタンの値を投稿する必要があります。問題は、serialize() がボタンの値をポストしないことです。私のフォームには、SAVE、UPDATE、および DELETE ボタンがあります。PHP は、どのボタンをクリックして特定の操作を行うかを知る必要があります。ボタンの値は click 関数で取得できますが、Ajax で PHP ページに渡す方法です。私のコードは以下にあり、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');
}
});
});
});
});