0

i have a select option, after selecting a brand I successfully get/display the brand name but I want also the other values be displayed like price, with or without refreshing the page.

<?php
require_once 'config.php';
echo '<select class="form-control action" name="tran_description" value="<?
php echo $tran_description; ?>" style="background-color:#F0F0F0">';
$sql = mysqli_query($mysqli, 'SELECT * FROM products order by product_id');
while ($row = $sql->fetch_assoc()){
echo '<option id="' . $row['product_id']  . '"';
echo ' value="' . $row['description'] . '" ';
echo ' value="' . $row['price'] . '" ';

if($row['description'] == $tran_description) {
if($row['price'] == $tran_price) {
$tran_price =  $row['price'];
echo ' selected="selected"'; 
}
}

if($row['product_id'] == $row['description']) {

echo ' selected="selected"'; 
}

echo '>';
echo $row['description'];
echo '</option>';         

}
echo '</select>';

?>

I can get the value of the description but the price I couldnt. In one select option representing the brand or description I want also the price value of that brand I selected be assign to a variable so I can do arithmetic operation in the back code without seeing it.Thanks.

Romeo
  • 125
  • 1
  • 13

1 Answers1

0

You are using wrong HTML structure only the first attribute is being used while the other is being ignored in the DOM .The right way to store multiple values in a single select can be like this:

<select name="">
    <option value="{'num_sequence':[0,1,2,3]}">Option one</option>
    <option value="{'foo':'bar','one':'two'}">Option two</option>
</select>

In your case it should be like this:

echo '<option id="' . $row['product_id']  . '"';
// echo ' value="{ /"description:/"' . $row['description'] . ', /"price:/"' . $row['price'] . ' }" ';
echo ' value="' . 
    json_encode(['description'=>$row['description'], 'price'=>$row['price']) . 
    '">";

Then to get the value you'll do is:

$description = $_POST['NameOfSelectInput']['description'];
$price = $_POST['NameOfSelectInput']['price'];

If you want to get the value in javascript, using jQuery, for example, you could do:

var value = JSON.parse($('#select_id').val()); // use id of your select control
var price = value.price;
var description = value.description;

You should do appropriate error checking...

For more details refer here: Can an Option in a Select tag carry multiple values?

RichGoldMD
  • 1,252
  • 1
  • 10
  • 18
Black Mamba
  • 13,632
  • 6
  • 82
  • 105
  • What I mean is, in one select option representing the brand or description I want also the price value of that brand I selected be assign to a variable so I can do arithmetic operation in the back code without seeing it. – Romeo Jul 30 '17 at 08:50
  • That's what he is suggesting. I would amend by using json_encode to encode the option value. To extract the data, use JSON.parse after select against the value (If displaying without reloading). The slashes are also forward slashes and should be back-slashes. – RichGoldMD Jul 30 '17 at 08:55
  • I tried to use your code Islan Mahajan, but still I cant. – Romeo Jul 30 '17 at 09:38
  • Is there other ways? – Romeo Jul 30 '17 at 10:06
  • By the way, Ishann, Im trying to figure out your code above the description is saving a value with only these { / not the description name? – Romeo Jul 30 '17 at 10:50