identifying form controls from php script

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
omkar
Forum Newbie
Posts: 1
Joined: Fri Jul 12, 2002 10:35 am

identifying form controls from php script

Post by omkar »

If I do not know the names of textfields on a form, how can I identify the names from within a PHP script specified in the 'action' property of the form.
User avatar
RandomEngy
Forum Contributor
Posts: 173
Joined: Wed Jun 26, 2002 3:24 pm
Contact:

Post by RandomEngy »

Give a name to all of your forms in the forms page, then in the php script that processes it will have all of the variables in $_REQUEST['name']. More specifically, if you used the "post" method to send the form, they will be in $_POST['name'] , and for the "get" method $_GET['name'] .

Example:

forms.html:

Code: Select all

<form action="process.php" method="post">
  <input type="text" name="mytext"><br>
  <input type="submit" name="submit" value="Submit the text">
</form>
process.php:

Code: Select all

<?php

echo "Your text is: " . $_POST&#1111;'mytext'];

?>
But I guess if you for some reason are unable to give the text fields names you could get the names of the fields by doing:

Code: Select all

foreach( $_POST as $key => $val )
  echo "Name of field: " . $key . "<br>\n";
Note: if the fields are not named, they are not passed to $_POST

Tell me if you need any elaboration.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

if you do not name the fields, the contents will not be transmitted since each data record for GET and POST is transmitted as key=value.
No name->no key->no value ;)
godfrey
Forum Newbie
Posts: 7
Joined: Fri Jul 12, 2002 9:46 am
Contact:

Post by godfrey »

instead of

echo "Your text is: " . $_POST['mytext'];

can`t you just use

echo "Your text is: $mytext";

I call all my field variables just putting the $ in front of the fieldname.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

You may. If your php.version is less than 4.2 or register_global has been explicitly turned on

http://www.devnetwork.net/forums/viewtopic.php?t=511
Post Reply