gpt4 book ai didi

javascript - jQuery Ajax POST 保存删除提交

转载 作者:太空宇宙 更新时间:2023-11-04 15:48:23 25 4
gpt4 key购买 nike

这是我的代码

<form method="post" role="form" id="form" enctype="multipart/form-data"  autocomplete="off">

<input type="submit" id="save" name="save" value="Simpan Data Client" class="btn" style="font-size:0.7em; letter-spacing:1px; color:#666666" /> //For save
<input type="submit" id="delete" name="delete" value="Delete Client" class="btn-delete" style="font-size:0.7em; letter-spacing:1px; color:#666666; padding:8px 15px" /> //For Delete
</form>
<script type="text/javascript">
$("#form").on("submit",function (e)
{
e.preventDefault();
var formData = new FormData($(this)[0]);
$.ajax(
{
url:'Master/Database/Client/RunClient.php',
type: 'POST',
data: formData,
contentType: false,
enctype: 'multipart/form-data',
processData: false,
beforeSend:function()
{
document.getElementById("loading").style.display = "";
},
complete:function()
{
//document.getElementById("loading").style.display = "none";
},
success:function(result)
{
document.getElementById("info").innerHTML = result;
var n = result.search("error");
if(n < 0) /*document.getElementById("form").reset();*/ $(".input").val('');

}
});
});
</script>

我可以从表单内部获取所有数据,除了我提交的输入类型之外。我无法在 RunClient.php 中使用 isset($_POST["save"]) 和 isset($_POST["delete"])

最佳答案

为提交创建单独的函数,并根据单击的按钮传递“提交类型”;

$('#save').click(function() {
submitForm('save');
});
$('#delete').click(function() {
submitForm('delete');
});
function submitForm(submittype) {
var formData = new FormData();
//push your form data to formData and add the submittype
formData['type'] = submittype
}

在你的 php 文件中

$submittype = $_POST['type']; // 'save' or 'delete'
if($submittype == 'save') {
//do save action
}
if($submittype == 'delete') {
//do delete action
}

关于javascript - jQuery Ajax POST 保存删除提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43364012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com