Skip to content Skip to sidebar Skip to footer

Onclick To Submit A Form

There is a PHP-generated HTML 4 transitional page that is used to edit data from a database of a single record. The user has two options: to store changes or delete the record. I u

Solution 1:

The form should submit according to your code. The only thing I spot is that you should terminate the input tags with />.

BUT... this way, even if the the confirm is cancelled, the form will be submitted. Use the form.onsubmit handler and if that returns false, the form will not submit.

I dont think @B3aT's answer is right in that not unconditionally the best way to "externalise" so to say. Many the the simplest is the best.

Solution 2:

I think the best way is to "externalize" the actual form posting.

//make regular buttons (not submit) //call your own functions (save and delete) //after you have done your logic do document.forms["myform"].submit();

Another solution is to add a checkbox named "delete" and rename the "save" button to "Done or do". And on server side, if "delete" is activated, then ..delete it.

Usually the "delete" is required "per entry" level (same user have multiple records), so you will have to make a separate button/link and eventually do an ajax request/access an URL with ?delete=1&id=3.

You need to make custom yes/no windows or use a jQuery plugin for it, the only browser standard is "confirm".

Solution 3:

OK, it worked, and this was in fact very stupid mistake. The problem was with this button as it was outside the form. I was so sure that I have it inside that I did not review PHP code but copied all from script not the HTML output as I should have done.

As I understand this correctly the line document.forms[0].submit(); worked but it was not because it was button who submitted the form but document.form[0] object itself.

Thank you for all your answers. I will try this form.onsubmit hint from Marcell.

Post a Comment for "Onclick To Submit A Form"