1

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.

3 Answers 3

4

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];
Sign up to request clarification or add additional context in comments.

6 Comments

This is what I was going to put as an answer, you may want to include the explode statement as well in case he doesn't know how to use it.
Also put a dot between $destination_place'">'
Edited the answer as suggested
Even though concatenating the value to ID is an easy method, you need to be careful, since the $destination_place looks like a Text entity and can contain quotes or > < symbols, so have to encode these
Thank you @Piancentecristian. the explode worked excellently. this was blast.
|
2

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'];

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.