Validating input with multiple submit buttons?
Posted: Thu Jan 20, 2011 8:07 am
I'm in the process of making my code a (little) bit more sophisticated, so I'm moving from type="submit" to type="button" with an onclick event, and then calling submit in the function. So far so good.
However, on this one form I have lots of submit buttons. In the old version, the php script which is used for onsubmit checked to see if it had been posted a variable with the name of each submit button and did the right thing accordingly. With the onclick event in the middle, this variable doesn't seem to be set any more.
I can see how to rewrite it so it would work (passing a different argument to my onclick event for each button and setting a hidden field based on that, and then checking the value of the hidden field's variable instead of isset for the button names), but is there a nice simple way to pass the "I was clicked" button variable value through without needing the rewrite?
Dead simple code sample:
which has replaced
and then the php script which it's sent to does
However, on this one form I have lots of submit buttons. In the old version, the php script which is used for onsubmit checked to see if it had been posted a variable with the name of each submit button and did the right thing accordingly. With the onclick event in the middle, this variable doesn't seem to be set any more.
I can see how to rewrite it so it would work (passing a different argument to my onclick event for each button and setting a hidden field based on that, and then checking the value of the hidden field's variable instead of isset for the button names), but is there a nice simple way to pass the "I was clicked" button variable value through without needing the rewrite?
Dead simple code sample:
Code: Select all
function donew(form)
{
...
form.submit();
}
...
<input type="button" id="button1" name="button1" value="Button1" onclick="donew(this.form)" />
<input type="button" id="button2" name="button2" value="Button2" onclick="donew(this.form)" />
Code: Select all
<input type="submit" id="button1" name="button1" value="Button1" />
<input type="submit" id="button2" name="button2" value="Button2" />
Code: Select all
<?php
@$button1 = $_POST['button1'];
@$button2 = $_POST['button2'];
if (isset($button1))
...
?>