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

This is a discussion on How PHP $_POST gets the multiple values from a HTML form? within the PHP Language forums, part of the PHP Programming Forums category; I am try test a simple HTML form with PHP <form method=POST action="testing.php"> ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-18-2005
RC
 
Posts: n/a
Default How PHP $_POST gets the multiple values from a HTML form?

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!
Reply With Quote
  #2 (permalink)  
Old 05-18-2005
RC
 
Posts: n/a
Default Re: How PHP $_POST gets the multiple values from a HTML form?

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);
> ?>
>

Reply With Quote
  #3 (permalink)  
Old 05-18-2005
Chris
 
Posts: n/a
Default Re: How PHP $_POST gets the multiple values from a HTML form?

On Wed, 18 May 2005 09:34:36 -0400, RC <raymond.chui@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


Reply With Quote
  #4 (permalink)  
Old 05-18-2005
Ewoud Dronkert
 
Posts: n/a
Default Re: How PHP $_POST gets the multiple values from a HTML form?

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/
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 11:51 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0