473,509 Members | 2,528 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How PHP $_POST gets the multiple values from a HTML form?

RC
I am try test a simple HTML form with PHP

<form method=POST action="testing.php">
Cat <input type="checkbox" name="pet" value="cat">
Dog <input type="checkbox" name="pet" value="dog">
Pig <input type="checkbox" name="pet" value="pig">
<br>
<select name="dates" multiple>
<option value="Monday">Monday</option>
<option value="Tuesday">Tuesday</option>
<option value="Wednesday">Wednesday</option>
<option value="Thursday">Thursday</option>
<option value="Friday">Friday</option>
<option value="Saturday">Saturday</option>
<option value="Sunday">Sunday</option>
</select><br>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form>

When I checked cat, pig and selected Tuesday and Saturday from the form
and click submitt

My testing.php is

<?php
$pet = $_POST['pet'];
$dates = $_POST['dates'];

print_r($pet);
var_dump($dates);
?>

The $_POST['pet'] and $_POST['dates']
are NOT return as array nor multiple
strings seperated with a comma.
It only return the LAST string I have
checked and/or selected.

How do I get the multiple values from the
checkbox and the multiple select from a
HTML form in PHP?

Thank Q very much in advance!
Jul 17 '05 #1
3 78674
RC
RC wrote:
I am try test a simple HTML form with PHP

<form method=POST action="testing.php">
Cat <input type="checkbox" name="pet" value="cat">
Dog <input type="checkbox" name="pet" value="dog">
Pig <input type="checkbox" name="pet" value="pig">
Never mind, I found the solution
In HTML file form need to changed to

Cat <input type="checkbox" name="pet[]" value="cat">
Dog <input type="checkbox" name="pet[]" value="dog">
Pig <input type="checkbox" name="pet[]" value="pig">
<br>
<select name="dates" multiple>
and need to changed to
<select name="dates[]" multiple>

<option value="Monday">Monday</option>
<option value="Tuesday">Tuesday</option>
<option value="Wednesday">Wednesday</option>
<option value="Thursday">Thursday</option>
<option value="Friday">Friday</option>
<option value="Saturday">Saturday</option>
<option value="Sunday">Sunday</option>
</select><br>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form>

When I checked cat, pig and selected Tuesday and Saturday from the form
and click submitt

My testing.php is

<?php
$pet = $_POST['pet'];
$dates = $_POST['dates'];
Then in PHP need to changed to

$pet = array($_POST['pet']);
$dates = array($_POST['dates']);
print_r($pet);
var_dump($dates);
?>

Jul 17 '05 #2
On Wed, 18 May 2005 09:34:36 -0400, RC <ra**********@nospam.noaa.gov>
wrote:
Try amending the slect tag to include a set of square brackets after
the name like this:

<select name="dates[]" multiple>

That should then give you the selected elements in an array.

hth

C
Jul 17 '05 #3
On Wed, 18 May 2005 09:43:32 -0400, RC wrote:
Then in PHP need to changed to

$pet = array($_POST['pet']);
$dates = array($_POST['dates']);


No way dude, remove the array(), then $pet will still be an array
because $_POST['pet'] is. By the way, no need to use a local variable
like $pet, you could just use $_POST['pet']. Try print_r($_POST);
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4104
by: r0adh0g | last post by:
I am attempting to build a primitive search form on my site. It is searching an Access Database Table and comparing on field in the database to a field passed from the form. Works great if only...
16
2858
by: Philippe C. Martin | last post by:
Hi, I am trying to change the data in a form field from python. The following code does not crash but has no effect as if "form" is just a copy of the original html form. Must I recreate the...
3
3707
by: Kathy | last post by:
Can someone help me with the code to take the data in four fields on an HTML form and add it to a table in a database on an intranet. Thanks to all who help! Kathy
9
2178
by: Bob Malcoprs | last post by:
Hi, I want to get all the selected values of a listbox in several labels.. With this code here below, when i click on e.g. "option 2", i get '2' in label2. That's ok. But if i then click on...
0
1084
by: pplers | last post by:
Is it possible to do this with VB6, instead of .NET ??? If it can be done, how ???
2
2643
by: kuchel | last post by:
Hi I have a table which looks typically like this:- ColA COLB 2370 2875 2370 3194 2376 3108 2417 2437 2539 2774 2539 2732 2539 3240
2
7838
by: Danielle | last post by:
Hello all, I'm pretty new to asp.net, I've to do a quick port to ASP.net I've got a problem I cannot solve: cannot find anything helpfull on the web. I've got an HTML form generated from a...
2
7999
by: =?Utf-8?B?Q29yeSBKLiBMYWlkbGF3LCBCZXlvbmQwMS5jb20= | last post by:
Hello! I need to retrieve POST information from an HTML form into my VB ASP.net 2008 web form. My simple html form is: <form action="webform1.aspx" method="post" name="Form1"> <input...
4
3059
by: Akpo | last post by:
Hi all...i am working on a project to capture students' data for an educational institution. I want to have a form that allows multiple student records to be filled in at once..e.g(this is the form):...
0
7233
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7135
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7342
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5650
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4729
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3201
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
440
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.