Page 1 of 1
How to know which button was clicked
Posted: Fri Feb 13, 2009 12:21 pm
by shoaibmohammeda
Hello,
As every one says, Im pretty new to PHP. I was going through a tutorial & suddenly got stucked up in a problem.
Supposingly in a form i have two buttons "Login","Sign Up" in a SAME PAGE
How do i know which button was clicked?
Hoping of getting guidance from you guys
Thank You
Re: How to know which button was clicked
Posted: Fri Feb 13, 2009 2:01 pm
by sparrrow
The submit buttons have
name and
value attributes that work the same as the ones for other form inputs. Assign a unique name and/or value to each button, and whichever one is clicked will have it's value assigned to the corresponding name key in the post data.
Form
Code: Select all
<input type="submit" name="submit" value="Sign Up" />
target POST page
Code: Select all
echo $_POST['submit']; //Returns "Sign Up"
Re: How to know which button was clicked
Posted: Fri Feb 13, 2009 4:09 pm
by pickle
Only the submit button that was clicked will appear in $_POST.
Re: How to know which button was clicked
Posted: Fri Feb 13, 2009 5:11 pm
by bugrush
You can check which button was clicked like this:
Code: Select all
if(isset($_POST['sign_up'])) {
//signup
} elseif(isset($_POST['login'])) {
//login
}
And about form buttons: I prefer using <button type="submit">Button Caption</button> over <input type="submit" value="Button Caption" /> because I like to keep button captions and values separate. If you use input for button then you can't really use it's value in php.
This feels cleaner:
Code: Select all
<button type="submit" name="action" value="sign_up">Sign Up</button>
<button type="submit" name="action" value="login">Login</button>
Re: How to know which button was clicked
Posted: Sat Feb 14, 2009 7:26 am
by shoaibmohammeda
Thank you guyz..
I dint expect such a help..
Really thank you.