×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

FORMS

How do I access form elements from a form in my PHP scripts? by inlandpac
Posted: 2 Oct 00

PHP automatically converts name elements in forms to variables!

Examples:

#########################################
form element:
<input type="text" name="first_name" value="My Name">

PHP interpretation:
$first_name ="My Name";

#########################################

form element:
<select name="age">
  <option value="15">15</option>
  <option value="25">25</option>
  <option value="30" selected>30</option>
  <option value="35">35</option>
</select>


PHP interpretation:
$age ="30";

#########################################

form element:
a.) <input type="checkbox" name="agree" value="YES">
b.) <input type="checkbox" name="agree" value="YES" checked>

PHP interpretation:
a.) $agree is not set
b.) $agree = "YES";


          NEW!

#########################################
MULTIPLE SELECT FORM ELEMENTS
#########################################

Just recently I was asked to add to this FAQ a section about multiple select form elements, so here it is!

Sometime in your development career or hobby, you will come across the need to use multiple select form elements (multiple select dropdowns) where people can select more than one item from a dropdown.

We all know that PHP converts parameters to variables automatically.  But what happens when you try to use a multiple select form element?  Well, if you try to do the following:


<form action="/mylocation/script.php" method="get">
  <select name="selection" multiple>
    <option value="computer">computer</option>
    <option value="monitor">monitor</option>
    <option value="camera">camera</option>
    <option vaue="printer">printer</option>
  </select>
  <input type="submit" name="submit" value="submit">
</form>


and we chose monitor and printer then submitted our request, the PHP script script.php to test it:


<?php
while(list($key,$var) = each($http_GET_VARS))
     if(is_array($var)) {
          for($a=0;$a<count($var);$a++)
               echo $key." = ".$var[$a]."<br>\n";
          
     }
     else {
          echo $key." = ".$var."<br>\n";
     }

?>


would read:


product = printer
submit = submit


Why did it not recognize monitor?

As we know, PHP automatically converts parameters to variables.  Since selecting multiple items from one selection box, we are essentially creating an array.  BUT, PHP does not know this and when we pass selection=monitor&selection=printer&submit=submit, $selection is set to monitor and then $selection is reset to printer.

Here is how we correct this:

We have to inform PHP that we are creating an array so that each time an item is passed to selection, it is really pushed into the end of the array.  And, since PHP automatically converts parameters to variables, we can tell PHP that selection is an array by changing

<select name="product" multiple>

to

<select name="product[]" multiple>

Now, when the variable is created it is actually $selection[] (which of course is an array.  Now when we submit our request we will get:


product = monitor
product = printer
submit = submit


Remember that product is an array and you have to step through the array to access its elements (notice the section in script.php (our form parser) that has:


     if(is_array($var)) {
          for($a=0;$a<count($var);$a++)
               echo $key." = ".$var[$a]."<br>\n";
     }


If you did not do this, you would see product = Array

Back to PHP FAQ Index
Back to PHP Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close