Page 1 of 1

Validating input with multiple submit buttons?

Posted: Thu Jan 20, 2011 8:07 am
by skylark2
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:

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)"  />
which has replaced

Code: Select all

<input type="submit" id="button1" name="button1" value="Button1"  />
<input type="submit" id="button2" name="button2" value="Button2"  />
and then the php script which it's sent to does

Code: Select all

<?php
  @$button1 = $_POST['button1'];
  @$button2 = $_POST['button2'];
  if (isset($button1))
...
?>

Re: Validating input with multiple submit buttons?

Posted: Fri Jan 21, 2011 2:40 pm
by John Cartwright
If you actually want the form to submit, then you probably want keep your buttons as a submit type, and have your javascript restrict the form submission or let it submit normally. I.e.,

Code: Select all

function donew(form)
{
   if (validation_fails()) {
      return false; //will stop the form from submitting
   }
   return true;
}

...
<input type="submit" id="button1" name="button1" value="Button1" onclick="return donew(this.form)"  />
<input type="submit" id="button2" name="button2" value="Button2" onclick="return donew(this.form)"  />

Re: Validating input with multiple submit buttons?

Posted: Sun Jan 23, 2011 12:08 pm
by remshad
can i use Ajax for validate submission form

Re: Validating input with multiple submit buttons?

Posted: Sun Jan 23, 2011 12:10 pm
by John Cartwright
remshad wrote:can i use Ajax for validate submission form
Yes.. but don't hijack some else's thread... :x

Re: Validating input with multiple submit buttons?

Posted: Wed Jan 26, 2011 3:23 am
by skylark2
So I can have an onclick even though it's type="submit"? That sounds like exactly what I need. Thanks - will go experiment.

The problem with learning from reading online is that it's dead easy to go off down a sidetrack and not come back. I'd read a tutorial about how you could do all these more sophisticated things if you changed "submit" to "button", and they used form.submit in the function, and it hadn't occurred to me that I could have the function without the change to "button".

Thanks again.

John Cartwright wrote:If you actually want the form to submit, then you probably want keep your buttons as a submit type, and have your javascript restrict the form submission or let it submit normally. I.e.,

Code: Select all

function donew(form)
{
   if (validation_fails()) {
      return false; //will stop the form from submitting
   }
   return true;
}

...
<input type="submit" id="button1" name="button1" value="Button1" onclick="return donew(this.form)"  />
<input type="submit" id="button2" name="button2" value="Button2" onclick="return donew(this.form)"  />