Forum Moderators: coopster

Message Too Old, No Replies

Passing Values to Hidden Field from Multiple List

Passing Values to Hidden Field from Multiple List

         

bbunny

7:30 pm on Dec 17, 2007 (gmt 0)

10+ Year Member



I have a list:

<select name="state" size="5" multiple="MULTIPLE" id="state">
<?php
do {
?>
<option value="<?php echo $row_Recordset2['state']?>"><?php echo $row_Recordset2['state']?></option>
<?php
} while ($row_Recordset2 = mysql_fetch_assoc($Recordset2));
$rows = mysql_num_rows($Recordset2);
if($rows > 0) {
mysql_data_seek($Recordset2, 0);
$row_Recordset2 = mysql_fetch_assoc($Recordset2);
}
?>
</select>

but need to pass multiple select values to hidden field:

<input type="hidden" name="hiddenField" value="<?php print $state?>" />

Upon selecting multiple values I get them returned in the URL:

http://www.example.com/test2.php?state=Alaska&state=Alabama

but only the last chosen state is returned in the hidden field such as Alabama in the above example.

[edited by: eelixduppy at 10:16 pm (utc) on Dec. 17, 2007]
[edit reason] delinked [/edit]

d40sithui

4:10 pm on Dec 18, 2007 (gmt 0)

10+ Year Member



you're getting ony the "Alabama" b/c you have two superglobal variables in the URL with the same name ("state"). because of this, the last one seems to overide all previous. consider making separate <Select> drop downs with different names.

cameraman

4:34 pm on Dec 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Add [ and ] brackets to then end of your form field name so you can process the selection as an array:
<select name="state[]" . . .

To move them along into a hidden field I think you'll need to string them together with a delimiting character. In general this would be a character that would never be a part of the data, which in this case could be anything that's not part of the alphabet. In the section of the script that receives the selection, do:

$states = implode [php.net](',',$_GET['state']);

then

<input name="selectedstates" type="hidden" value="<?php echo $states;?>">

In the script that processes that form (assuming method="get" again):

$state = explode(',',$_GET['selectedstates']);

to get them back into an array.