Javascript 通过json更新数据库
我正在使用以下代码编辑数据库中的记录。它非常适合做编辑。我需要扩展功能,以包括添加或删除功能也 在我的表单上,我使用三个单独的按钮来选择是添加、编辑还是删除 如何展开脚本以确定单击了哪些按钮Javascript 通过json更新数据库,javascript,json,edit,Javascript,Json,Edit,我正在使用以下代码编辑数据库中的记录。它非常适合做编辑。我需要扩展功能,以包括添加或删除功能也 在我的表单上,我使用三个单独的按钮来选择是添加、编辑还是删除 如何展开脚本以确定单击了哪些按钮 // Action to run on submit to add/edit/delete record from database var request; // variable to hold request // bind to the submit event of our form $("#f
// Action to run on submit to add/edit/delete record from database
var request; // variable to hold request
// bind to the submit event of our form
$("#form_edit").submit(function(event){
// abort any pending request
if (request) {
request.abort();
}
// setup local variables
var $form = $(this);
// select and cache all the fields
var $inputs = $form.find("input, select, textarea");
// serialize the data in the form
var serializedData = $form.serialize();
// disable the inputs for the duration of the ajax request
$inputs.prop("disabled", true);
// define constants to be passed as parameters
var updateType = "edt"; // This needs to be changed to 'add' or 'del'
var tableName = "phasecodes";
var indexName = "phase_ID";
var recNumber = phase_ID.value;
// Build string for query input
var type = "?type=" + updateType;
var table = "&table=" + tableName;
var index = "&index=" + indexName;
var rec = "&rec=" + recNumber;
var getS = type + table + index + rec;
// execute request to update database
request = $.ajax({
url: "files_json/update_Record.php" + getS,
type: "post",
data: serializedData
});
// callback handler called on success
request.done(function (response, textStatus, jqXHR) {
console.log("Data record updated!"); // log success message to the console
});
// callback handler called on failure
request.fail(function (jqXHR, textStatus, errorThrown) {
console.error( "The following error occured: " + textStatus, errorThrown ); // log the error to the console
});
// callback handler called regardless if the request failed or succeeded
request.always(function () {
$inputs.prop("disabled", false); // reenable the inputs
});
// prevent default posting of form
event.preventDefault();
}); // End of submit function
假设表单中的三个按钮类型为“提交…” 你可以看到 类似这样的内容应返回单击按钮的值:
var val = $("input[type=submit][clicked=true]").val();