Page 1 of 1

identifying form controls from php script

Posted: Fri Jul 12, 2002 10:35 am
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.

Posted: Fri Jul 12, 2002 11:15 am
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.

Posted: Fri Jul 12, 2002 11:34 pm
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 ;)

Posted: Sat Jul 13, 2002 9:00 am
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.

Posted: Sat Jul 13, 2002 3:40 pm
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