detecting which of 2 buttons was hit.
Started by high6, Jun 27 2006 03:02 PM
7 replies to this topic
#1
Posted 27 June 2006 - 03:02 PM
ok well i got
<form action="add.php" method="post">
<p><textarea name="add" rows="2" cols="50"></textarea></p>
<p><input type="submit" value="Submit"></p>
</form>
in html and i was wondering how do i make it so there are 2 buttons 1 submit and 1 preview and how do i tell which one was hit in php?
<form action="add.php" method="post">
<p><textarea name="add" rows="2" cols="50"></textarea></p>
<p><input type="submit" value="Submit"></p>
</form>
in html and i was wondering how do i make it so there are 2 buttons 1 submit and 1 preview and how do i tell which one was hit in php?
#2
Posted 27 June 2006 - 03:23 PM
[quote name='high6' post='148221' date='Jun 27 2006, 03:01 PM']ok well i got
<form action="add.php" method="post">
<p><textarea name="add" rows="2" cols="50"></textarea></p>
<p><input type="submit" value="Submit"></p>
</form>
in html and i was wondering how do i make it so there are 2 buttons 1 submit and 1 preview and how do i tell which one was hit in php?[/quote]
And I'm not sure how your add.php is set up, but I'll assume you'll want something like this.
I'm not quite sure if you are allowed to use two submit buttons, but that would activate the add.php script. If it doesn't allow you to do that, you'll have to change the Preview button into a normal button input and hyperlink it or something.
Something like that should give you a good start atleast.
<form action="add.php" method="post">
<p><textarea name="add" rows="2" cols="50"></textarea></p>
<p><input type="submit" value="Submit"></p>
</form>
in html and i was wondering how do i make it so there are 2 buttons 1 submit and 1 preview and how do i tell which one was hit in php?[/quote]
And I'm not sure how your add.php is set up, but I'll assume you'll want something like this.
I'm not quite sure if you are allowed to use two submit buttons, but that would activate the add.php script. If it doesn't allow you to do that, you'll have to change the Preview button into a normal button input and hyperlink it or something.
Something like that should give you a good start atleast.
Edited by Demonslay, 27 June 2006 - 03:26 PM.
#3
Posted 27 June 2006 - 03:42 PM
Actually, that wouldn't work to well as you're missing one key attribute. The name attribute.
<form action="add.php" method="post"> <p><textarea name="add" rows="2" cols="50"></textarea></p> <p><input type="submit" name="button" value="Submit"></p> <p><input type="submit" name="button" value="Preview"></p> </form>
<?php
if($_POST['button'] == 'Preview'){
// Do something if the Preview button was pressed, most likely displaying the data and returning the form
}elseif($_POST['button'] == 'Submit'){
// Do something if the Submit button was pressed, most likely adding to the database
}else{
// Execute some error code saying neither button was pressed, just incase someone tries to access the page directly
}
?>
In the PHP, all i did was add single quotes where they belonged, changed the $_POST's key, and thats about it.
#4
Posted 27 June 2006 - 05:16 PM
Someone told me that the proper way to use superglobals is to not put the key in quotes at all (can't remeber why, I'll look), and he knows alot about PHP and anything I could think of, so that's what I've always done.
And for some reason at that moment I thought it classified by the type. Thanks for the catch.
Quote from nos_slived.
That's from a long time ago on CodingForums.
And for some reason at that moment I thought it classified by the type. Thanks for the catch.
Quote from nos_slived.
Quote
$_POST[] is an array, which is properly signified by Array[], not Array[''], so Demonslay was right in his syntactical grammar.
That's from a long time ago on CodingForums.
Edited by Demonslay, 27 June 2006 - 05:22 PM.
#5
Posted 28 June 2006 - 02:04 AM
I hope nobody gets offended by this, but who ever said that is a blatant idiot.
http://php.net/manual/en/language.types.ar...pes.array.donts
There are 2 kinds of arrays. Multi-dimensional and regular, regardless of how many arrays are with-in the arrays, it’s an array.
Anyway, I hope I don't seem like a nazi right about now, but your welcome for the catch on the name thing. Just fyi, type is strictly html, php doesn't care what the type of something is, nor are you ever able to find out what it is. The name and value attributes are all that matter
http://php.net/manual/en/language.types.ar...pes.array.donts
There are 2 kinds of arrays. Multi-dimensional and regular, regardless of how many arrays are with-in the arrays, it’s an array.
Anyway, I hope I don't seem like a nazi right about now, but your welcome for the catch on the name thing. Just fyi, type is strictly html, php doesn't care what the type of something is, nor are you ever able to find out what it is. The name and value attributes are all that matter
#6
Posted 28 June 2006 - 08:38 AM
More on topic, i have tended to code like $_POST[foo] but started to get back into the habbit of $_POST['bar']... I dont fully understand the complex of it all but should stick to what the manual says.
#7
Posted 28 June 2006 - 12:40 PM
Well, that's a good find, but i don't think it applies as i was using the term nazi to imply strict, must have it my way behavior, not something that's not liked. And, i don't see how that can be a law as it's more of a theory in my opinion. Regardless, i will keep it in mind.
As for sticking on topic... I won't force somebody to change the way they write their code, but i will say that in this particular case, it is a lot less likely that problems will arise if you do what php.net says for this.
And again, not to be "strict," unless somebody has something more useful to add, or we still have a problem, this topic is solved. But you're welcome to open a debat on godwin's "law" in general discussion
As for sticking on topic... I won't force somebody to change the way they write their code, but i will say that in this particular case, it is a lot less likely that problems will arise if you do what php.net says for this.
And again, not to be "strict," unless somebody has something more useful to add, or we still have a problem, this topic is solved. But you're welcome to open a debat on godwin's "law" in general discussion
Edited by rc69, 28 June 2006 - 12:40 PM.
#8
Posted 28 June 2006 - 02:16 PM
rc69, on Jun 28 2006, 08:04 AM, said:
I hope nobody gets offended by this, but who ever said that is a blatant idiot.
lol, thats what i thought the when i read it. I didn't post because honestly i didn't know why it was right exactly....knew it was though
And if its in the php manual, then foo to whoever says otherwise
Edited by .Matt, 28 June 2006 - 02:17 PM.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users
