Acquiring multiple values from one select option value. PHP HTML

How do I have acquire multiple values from one select option value. An example here,

<html>

<form method="post" action="#">
   <select name="retrive_destination">
       <?php while($row = mysql_fetch_assoc($result)){
                    $destinationID = $row['destinationID'];
                    $destination_place = $row['destination_place'];
                    echo '<option value="'.$destinationID.'">'.$destination_place.'</option>
       ?>
      <input type="submit">
</select>

</form>
</html>

How do i get both values '$destinationID' and '$destination_place' from one option value , when the form i submitted as i need both values when the user selects an option on the next step. THank you guys cheers.

Answers


Compose the two values and then explode them server-side

<html>

<form method="post" action="#">
   <select name="retrive_destination">
       <?php while($row = mysql_fetch_assoc($result)){
                    $destinationID = $row['destinationID'];
                    $destination_place = $row['destination_place'];
                    echo '<option value="'.$destinationID."_".htmlentities($destination_place).'">'.$destination_place.'</option>
       ?>
      <input type="submit">
</select>

</form>
</html>

Php server-side

$value= $_REQUEST['retrive_destination'];
$explode=explode("_",$value,2);
$destinationID =$explode[0];
$destination_place =$explode[1];

Jquery Version:

Add an input type hidden on your form:

<form method="post" action="#">
<select name="retrive_destination" id="des">
   <option value="1">Ibiza</option>
   <option value="2">Mallorca</option>
   <option value="3">Chile</option>
</select>
<input type="hidden" name="destination" id="dest"/>
<input type="submit" />
</form>

Jquery:

$(document).ready(function(){
$("#des").change(function(){
   $("#dest").val($("#des option:selected").text());  
});
});

PHP:

$destId  = $_POST['retrive_destination'];
$destVal = $_POST['destination'];

Best is to have the form only submit the ID as you can always fetch the destination place from database in your server side.


Need Your Help

Add writing permission to PHP on IIS 7

php iis-7 windows-server-2008 file-permissions

I need a PHP script to have writing permission in a directory. PHP 5.3 is running as FastCGI under IIS 7 with windows server 2008 as OP. On my php error logs, I got "permission denied" when the scr...

How to launch MediaPlayerLauncher using device's isolated storage

c# windows-phone-7 xaml

I would like to simply launch the MediaPlayerLauncher task, but I would like to use media that is already on the WP7 device. From my research it seems that to use this task, one must first include ...