Hasyidan Paramananda
Hasyidan Paramananda

Reputation: 188

jQuery Ajax POST save delete submit

Here is my code

<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>

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

Upvotes: 1

Views: 1582

Answers (3)

arunraj.g
arunraj.g

Reputation: 1

'$("#form").on("submit",function (e)' replace the function with

$("#save").click(function() {

});

$("#delete").click(function() {

});

Upvotes: 0

Jordi Huertas
Jordi Huertas

Reputation: 93

I use to avoid submit inputs and change by buttons.

<button type="button" id="save">SUBMIT</button> //For save

<script type="text/javascript">
$("#save").on("click",function (e)
{

});
</script>

So, if anyone deativates javscript form will not submit.

And you can send the data like this:

data: {
    foo: 'var'
    foo2: 5
}, 

EDIT. Sorry missunderstood your question. Just control with javascript what button is clicked and assign a value with a hidden field.

Upvotes: 0

MakeLoveNotWar
MakeLoveNotWar

Reputation: 954

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
}

in your php file

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

Upvotes: 1

Related Questions