I have a form with several elements and two submit buttons: SAVE and DELETE. When submitting the form, via jQuery ajax, i need to know if the user clicked on DELETE so i can execute the code accordingly. The problem is that jQuery doesn't send the submit name and value. So, i've read here and found what it looks like the ideal workaround: use input for SAVE and a button for DELETE . With a DELETE button i can send the form with the name and value of the button, therefore execute the code for the delete. The problem is that im trying to tie the pieces together with no success. Here's my code:
HTML:
<form id="f-release" method="post" action="includes/submit_release.php">
<fieldset>
<input name="save" type="submit" value="save">
<button name="delete" type="button" value="delete" onClick="return confirm('Do you really wish to permanently delete the record?')">Delete</button>
</fieldset>
</form>
ajax:
$("form button[name=delete]").click(function() {
var formData = $(this).closest('form').serializeArray();
formData.push({name: this.name, value:this.value });
});
$('form#f-release').submit(function(event) {
event.preventDefault();
var form = $(this);
$.ajax({
type: form.attr('method'),
url: form.attr('action'),
data: form.serialize(),
dataType: "json"
}).done(function(data) {
alert('done');
}).fail(function(data) {
alert('error!');
});
});
php:
if (isset($_POST['releaseid']) && is_numeric($_POST['releaseid'])) {
// save
} elseif (...delete...) {
//delete
}
When i click delete, how do i submit the form with the name and value of the delete button so i can execute the php DELETE code correctly?