$_POST

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
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

$_POST

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

Post 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.
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post 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>
thallish
Forum Commoner
Posts: 60
Joined: Wed Mar 02, 2005 11:38 am
Location: Aalborg, Denmark

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

Post by feyd »

you can't bank on the submit button existing in the submitted data.
thallish
Forum Commoner
Posts: 60
Joined: Wed Mar 02, 2005 11:38 am
Location: Aalborg, Denmark

Post 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
Last edited by thallish on Thu Mar 31, 2005 1:46 pm, edited 2 times in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

If the user presses "enter"... no button was pressed, and the form is submitted
thallish
Forum Commoner
Posts: 60
Joined: Wed Mar 02, 2005 11:38 am
Location: Aalborg, Denmark

Post 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
Last edited by thallish on Thu Mar 31, 2005 1:50 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Post 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>
Last edited by anthony88guy on Thu Mar 31, 2005 2:00 pm, edited 1 time in total.
Serengeti
Forum Newbie
Posts: 19
Joined: Sat Jan 22, 2005 1:58 am

Post 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++;
thallish
Forum Commoner
Posts: 60
Joined: Wed Mar 02, 2005 11:38 am
Location: Aalborg, Denmark

Post 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
thallish
Forum Commoner
Posts: 60
Joined: Wed Mar 02, 2005 11:38 am
Location: Aalborg, Denmark

Post 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
Post Reply