There is a form:

<form id="spageForm" method="post" action="index.php"> <button type="submit" name="save"></button> <button type="button" name="delete"></button> </form> 

I need to use JS (preferably jQuery) to initialize the submit() event for <button type="button" name="delete"></button> .

At the moment, I found a way to $('#spageForm').submit(); submit the form: $('#spageForm').submit(); , but $_POST['delete'] not sent to index.php , but it needs to be sent.

  • <button type = "button | reset | submit"> ... </ button> from htmlbook - Jeque
  • I chose button, and not submit, so that when I clicked on the button, the form was not sent, but my code was executed, after which the event should work: as if I clicked a button from type = submit - Jeque
  • Yes, but without a click - Jeque
  • No, just delete. save I left in the example to show that I have two buttons. If it were, then the problem would be solved by specifying type = 'submit' for delete and onsubmit = "return false; for the form. But with two buttons it will not work this way - Jeque
  • Roughly speaking yes, it is - Jeque

2 answers 2

You can add a hidden field to the form:

 <form id="spageForm" method="post" action="index.php"> <input type="hidden" name="" id="form_action"> <button type="submit" name="save"></button> <button type="button" name="delete"></button> </form> 

And then the script will be:

 document.getElementById("form_action").name = "delete"; document.getElementById("form_action").value = "y"; $('#spageForm').submit(); 

Or just do

 var data = $('#spageForm').serialize(); $.ajax({ method: "POST", url: "index.php", data: data+"&delete=y" }) 
  • Thanks for the idea of ​​the hidden field, only I wrote that $ ('# spageForm'). Submit (); Doesn't work - Jeque
  • Well, I wrote in the script to add a hidden field and send the form, according to this scheme you will $_POST['delete'] == "Y" - Mihanik71
  • want to say that $ _POST ['save'] will not come? - Jeque
  • @EvgeniiZaets no, save will not come as the form is sent without pressing the save button. She just sends the current form parameters - Mihanik71
  • Sori has already started to blunt , in theory it should work - Jeque

Solved a problem. Added a hidden <button type="submit" name="del"></button> field and sent the form using $("button[name='del']").click();