<button> + isset()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PhpMachine
Forum Commoner
Posts: 42
Joined: Thu Apr 19, 2007 11:26 am

<button> + isset()

Post 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>
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Have two submit buttons. With different names.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
nhammond
Forum Newbie
Posts: 14
Joined: Mon Dec 18, 2006 11:35 am

Re: <button> + isset()

Post 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]
PhpMachine
Forum Commoner
Posts: 42
Joined: Thu Apr 19, 2007 11:26 am

Post 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" />
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Buttons don't have a type. They don't submit the page. So yes, you need to use input.
PhpMachine
Forum Commoner
Posts: 42
Joined: Thu Apr 19, 2007 11:26 am

Post 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
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

For every button you want to use in order to submit data use input element and submit type.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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?
PhpMachine
Forum Commoner
Posts: 42
Joined: Thu Apr 19, 2007 11:26 am

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply