Page 1 of 1

$_POST

Posted: Wed Mar 30, 2005 8:26 pm
by anthony88guy
I have 2 forms on 1 page, and I need an if statment like

Code: Select all

if($_POST){
blah
}
But the if statment is directed to one of the forms, for instance if one form was called dog and the other was called cat. If dog form was submitted then the if statment would be true, is this possible?

Posted: Wed Mar 30, 2005 8:40 pm
by feyd
The form's name is not transmitted when submitting. A simple way is to use either a unique variable name inside the form, or use a common name to one, but differing values.

Posted: Wed Mar 30, 2005 10:39 pm
by nickvd
feyd wrote:The form's name is not transmitted when submitting. A simple way is to use either a unique variable name inside the form, or use a common name to one, but differing values.
To provide an example (i'm nice, what can I say)... use a hidden input tag in the forms... i.e.

Code: Select all

<form>
   <input type=&quote;hidden&quote; name=&quote;formName&quote; value=&quote;form1&quote; />
</form>
blah
blah
<form>
   <input type=&quote;hidden&quote; name=&quote;formName&quote; value=&quote;form2&quote; />
</form>

Posted: Thu Mar 31, 2005 5:16 am
by thallish
hey

another way you can do it is to have two names for the submit buttons

Code: Select all

&lt;form method=&quote;post&quote; action=&quote;somepage.php&quote;&gt;

&lt;input type=&quote;submit&quote; name=&quote;dog&quote; value=&quote;Whatever&quote;

&lt;/form&gt;

&lt;form method=&quote;post&quote; action=&quote;somepage.php&quote;&gt;

&lt;input type=&quote;submit&quote; name=&quote;cat&quote; value=&quote;Whatever&quote;

&lt;/form&gt;
and then in the code on your somepage.php you can test to see whatever form it came from by using isset()

Code: Select all

if(isset($_POST['dog']))
{
   //do for dog
}

if(isset($_POST['cat']))
{
   //do for cat
}
regards

thallish

Posted: Thu Mar 31, 2005 12:19 pm
by feyd
you can't bank on the submit button existing in the submitted data.

Posted: Thu Mar 31, 2005 1:13 pm
by thallish
Why not? please elaborate :?

EDIT: I found another page where it is written that hitting 'enter' on the submit button isn't the same as clicking with the mouse.


thallish

Posted: Thu Mar 31, 2005 1:19 pm
by John Cartwright
If the user presses "enter"... no button was pressed, and the form is submitted

Posted: Thu Mar 31, 2005 1:45 pm
by thallish
ok now i'm a little confused once more :?

In the current project i'm working, hitting 'enter' when the submit button is higlighted results in the same as when i'm clicking with the mouse? Anybody know why that is the case? Is it some certain setting that have to be taken in to consideration?

If so i would appreciate it as it's getting closer to delivery time :wink:

thallish

Posted: Thu Mar 31, 2005 1:49 pm
by feyd
hitting enter or space on the button itself will submit as though clicking the button, typically. However, if you are in a text field, or a few others I can't remember, hitting enter may not send the submit button.. depends on the browser you are using.. IE doesn't send, Firefox does.

Posted: Thu Mar 31, 2005 1:57 pm
by anthony88guy
So the best way to do it would be

Code: Select all

<?php
if(isset($_POST['dog'])){
print "Bark";
}
if(isset($_POST['cat']))
print "meow";
}
?>
<form>
   <input type="hidden" name="dog" value="dog" />
</form>

<form>
   <input type="hidden" name="cat" value="cat" />
</form>

Posted: Thu Mar 31, 2005 2:01 pm
by Serengeti
feyd wrote:...hitting enter may not send the submit button.. depends on the browser you are using.. IE doesn't send, Firefox does.
$firefox++;

Posted: Thu Mar 31, 2005 2:03 pm
by thallish
hmm just tested it in IE and firefox and there seems to be no problem in that direction. If it turns out to be a problem i must correct it. Well now I just found another troublesome one but that's a different post

thallish

Posted: Thu Mar 31, 2005 2:05 pm
by thallish
anthony88guy wrote:So the best way to do it would be

Code: Select all

<?php
if(isset($_POST['dog'])){
print "Bark";
}
if(isset($_POST['cat']))
print "meow";
}
?>
<form>
   <input type="hidden" name="dog" value="dog" />
</form>

<form>
   <input type="hidden" name="cat" value="cat" />
</form>
that's probably the safest one. I just like trouble :wink:

thallish