简体   繁体   中英

PHP Multiple Hidden Values Select Option

Is it possible to assign multiple values to select option drop down lists? I need to retrieve multiple pieces of data from each drop down when they are selected and I have only been able to get the "name" and an "id". The name is displayed for the user to select however it is the ID that is passed to be processed. Here is my code:

<?php
$att_1 = $att_1;
mysql_connect("xx.xx.xx.xx","xxxxxx","xxxxxx");
mysql_select_db("dezanjow_cf");
$sql=mysql_query("select id, name from model");
if(mysql_num_rows($sql)){
$select= '<select name="model">';
$select.='<option value="default">Select Model</option>';
while($rs=mysql_fetch_array($sql)){
     $select.='<option value="'.$rs['id'].'">'.$rs['name'].'</option>';
  }
}
$select.='</select>';
echo $select;
?>

This produces html that looks like:

<select name="model">
<option value="default">Select Model</option>
<option value="5">GH20</option>
<option value="6">GH21</option>
<option value="7">GH22</option>
</select>

I wish to display them like this (as example):

<select name="model">
<option value="default">Select Model</option>
<option value="5","abc">GH20</option>
<option value="6","def">GH21</option>
<option value="7","ghi">GH22</option>
</select>

Thus when the data is passed onto the next php script, I can use both data "5" and "abc" when the user selects "GH20". I have not seen much on Google about this and don't even know if this is possible. Please let me know if I am asking for the impossible! Many Thanks, Nick

Hey you cannot have multiple values like that because post or get will return just first value which in this case is 5

<option value="5","abc">GH20</option>

but you can make your value looks like this

<option value="5,abc">GH20</option>

and then in php script you can separate values by using explode function

explode(',', $_POST['model']) which will return array that you can use array( 0=> 5, 1=>abc)

Its better if you serialize your data then at next php script unserialize it.

$data=array($rs['id'], "abc");   //data to be stored
$serialized_data=serialize($data);    //serialize data

$select.='<option value="'.$serialized_data.'">'.$rs['name'].'</option>';

IMPORTANT

//unserialize information
$unserialized_data=unserialize($serialized_data);

this will output the same array array('SOMEID', "abc")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM