jQuery Ajax POST保存删除提交 [英] jQuery Ajax POST save delete submit

查看:141
本文介绍了jQuery Ajax POST保存删除提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

<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"])

I can get all data from inside my form except from Input type submit i make. I can't use isset($_POST["save"]) and isset($_POST["delete"]) at my RunClient.php

推荐答案

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

Create separate function for a submit and pass "submit type" depending on what button is clicked;

$('#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
}

这篇关于jQuery Ajax POST保存删除提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆