| | |
HTML Select and PHP 5 - Issue
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Feb 2005
Posts: 31
Reputation:
Solved Threads: 2
My simple HTML script below uses SELECT to create a list which should be accessed by the PHP script as an array. Note the products[] definition below.
BUT.... I can't seem to get the script to read the array from $_POST['products'] - anyone know how to solve this?
<HTML>
<TITLE>Input Form2</TITLE>
<FORM method="POST" action="FormRead2.php">
<BR>Name:<BR>
<INPUT type="text" name="user">
<BR>Address:<BR>
<TEXTAREA name="address" rows="5" cols="40">
</TEXTAREA>
<BR>Products:<BR>
<SELECT name "products[]" multiple>
<option>SFone
<option>CTI Desktop
<option>Real-Time Monitor
</SELECT>
<BR>
<INPUT type="submit" name="submit" value="Submit">
</FORM>
</HTML>
And here is the simple PHP script:
<HTML>
<TITLE>Action Script for InputForm2</TITLE>
<?php
if(isset($_POST['submit'])) {
echo "Welcome <b>".$_POST['user']."</b><P>\n\n";
echo "Your address is:<P>\n\n<b>".$_POST['address']."</b><p>\n\n";
echo "Your product choices are:<p>\n\n";
echo "<UL>\n\n";
foreach($_POST['products'] as $value) {
echo "<LI>$value<BR>\n";
}
echo "</UL>";
}
?>
</HTML>
BUT.... I can't seem to get the script to read the array from $_POST['products'] - anyone know how to solve this?
<HTML>
<TITLE>Input Form2</TITLE>
<FORM method="POST" action="FormRead2.php">
<BR>Name:<BR>
<INPUT type="text" name="user">
<BR>Address:<BR>
<TEXTAREA name="address" rows="5" cols="40">
</TEXTAREA>
<BR>Products:<BR>
<SELECT name "products[]" multiple>
<option>SFone
<option>CTI Desktop
<option>Real-Time Monitor
</SELECT>
<BR>
<INPUT type="submit" name="submit" value="Submit">
</FORM>
</HTML>
And here is the simple PHP script:
<HTML>
<TITLE>Action Script for InputForm2</TITLE>
<?php
if(isset($_POST['submit'])) {
echo "Welcome <b>".$_POST['user']."</b><P>\n\n";
echo "Your address is:<P>\n\n<b>".$_POST['address']."</b><p>\n\n";
echo "Your product choices are:<p>\n\n";
echo "<UL>\n\n";
foreach($_POST['products'] as $value) {
echo "<LI>$value<BR>\n";
}
echo "</UL>";
}
?>
</HTML>
•
•
Join Date: Jul 2004
Posts: 11
Reputation:
Solved Threads: 1
•
•
•
•
Originally Posted by mcldev
<option>SFone</option>
<option>CTI Desktop</option>
<option>Real-Time Monitor</option>
•
•
Join Date: Feb 2005
Posts: 31
Reputation:
Solved Threads: 2
The Solution
In case you are interested, the key to getting the HMTL script to pass SELECT data is to 1) declare your variable as an array, i.e. use brackets like product[], 2) Then in your PHP action script make sure you treat product as an associative array, i.e. forech($_POST['product'] as $key=>$value). Note, this allows you to pass over multiple selections.
Also, note the use of _POST. If you are still using PHP4 it is important to look at _SERVER, _GET, etc. because you will undoubtly cross their path during forms development. You can use the long names like HTTP_POST_VARS but the shorter names are more managable.
In case you are interested, the key to getting the HMTL script to pass SELECT data is to 1) declare your variable as an array, i.e. use brackets like product[], 2) Then in your PHP action script make sure you treat product as an associative array, i.e. forech($_POST['product'] as $key=>$value). Note, this allows you to pass over multiple selections.
Also, note the use of _POST. If you are still using PHP4 it is important to look at _SERVER, _GET, etc. because you will undoubtly cross their path during forms development. You can use the long names like HTTP_POST_VARS but the shorter names are more managable.
![]() |
Similar Threads
- HTML codes in PHP (PHP)
- Sending HTML email via PHP mail function (PHP)
- View HTML source in php page? (PHP)
- Using HTML tags in PHP code (PHP)
- PDF to HTML conversion--php (PHP)
- storing details as html doc using php (PHP)
Other Threads in the PHP Forum
- Previous Thread: PHP File Searching
- Next Thread: Help Me In Font Conversion
Views: 10816 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl database date datepart directory display download dynamic echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla jquery limit link list login loop mail menu methods mlm mod_rewrite multiple mysql oop parse password paypal pdf php problem query radio random recursion regex remote script search select seo server sessions sms soap source space speed sql structure syntax system table tutorial update updates upload url validation validator variable video web webdesign xml youtube





