Page 1 of 1

<button> + isset()

Posted: Tue Sep 18, 2007 12:10 pm
by PhpMachine
Hi

How do I check which button is pressed?
isset() does not work on <button>, but it works for <input type='submit'>

Code: Select all

<form method='post'>
 <button type='submit' name='button1'>Button 1</button>
 <button type='submit' name='button2'>Button 2</button>
</form>

Posted: Tue Sep 18, 2007 1:48 pm
by s.dot
Have two submit buttons. With different names.

Re: <button> + isset()

Posted: Tue Sep 18, 2007 4:04 pm
by nhammond
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


[quote="PhpMachine"]Hi

How do I check which button is pressed?
isset() does not work on <button>, but it works for <input type='submit'>

Code: Select all

<form method='post'>
 <button type='submit' name='button1'>Button 1</button>
 <button type='submit' name='button2'>Button 2</button>
</form>
[/quote]

in your php script that you use to check you need to do something like this

Code: Select all

if (isset($_POST['button1'])) { echo "button 1 has been set and it's value is " . $_POST['button1'] ; }
if (isset($_POST['button2'])) { echo "button 2 has been set and it's value is " . $_POST['button2'] ; }
if you want to see if it isn't set then just do the same but put an exclamation mark in front of the isset() function
so

if (!isset($_POST['button1']))
would return true if button 1 is not set


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Sep 19, 2007 12:40 pm
by PhpMachine
Hi
I cant get isset() to work on my <button>s.

If I press the button with name="button1", then this statement is true:

Code: Select all

if(isset($_POST["button2"]))
Both isset() statements returns true for me.
It seems that $_POST["button1"] always returns "Button 1", and is therefore always set (and the equivalent for the second button).

Is the only solution to use this instead?:

Code: Select all

<input type="submit" />

Posted: Wed Sep 19, 2007 5:12 pm
by feyd
Buttons don't have a type. They don't submit the page. So yes, you need to use input.

Posted: Thu Sep 20, 2007 10:37 am
by PhpMachine
Hi Feyd
Buttons don't have a type. They don't submit the page. So yes, you need to use input.
The <button> element does have a attribute "type" and it does submit the page if no value is given, or set to "submit":
http://www.w3.org/TR/html4/interact/forms.html#h-17.5

Posted: Thu Sep 20, 2007 11:23 am
by aceconcepts
For every button you want to use in order to submit data use input element and submit type.

Posted: Thu Sep 20, 2007 5:58 pm
by Kieran Huggins
A set of radio buttons with a a default could work as well, but this has the smell of bad design. What are you trying to do exactly?

Posted: Fri Sep 21, 2007 2:08 pm
by PhpMachine
Im trying to use "isset()" to determine which <button> sent the form.
But it seems it doesn't work for the <button> element.

Posted: Fri Sep 21, 2007 4:15 pm
by pickle
As everyone else has said, use <input>. You can even name them the same, but give them different values.

For example:

Code: Select all

<input type = "submit" name = "myButton" value = "Button 1" />
<input type = "submit" name = "myButton" value = "Button 2" />
<input type = "submit" name = "myButton" value = "Button 3" />
If a user clicks the button with value "Button 3", then your $_POST['myButton'] will have a value of 'Button 3' and nothing else.