Beel
Beel

Reputation: 31

Using droplist from database set value of another droplist based on the selection of the first one

I have this table

Device Name    Price
iPhone 6       300
S8             600

What I am trying to do is have to drop list one that has all the devices Name and the second has an option "Price" but I want to set its value based on the selection of the device name. However, in the drop list it should just display "Price"

I have this php code for the drop lists

$query = "SELECT * FROM `devicehotsheet`";
$options = "";
$result1 = mysql_query($query, $dbhandle);
while ($row1= mysql_fetch_array($result1)){
$options=$options."<option>$row1[0]</option>";
}

For HTML

  <select class="selectDevice" id="selectDevice" onChange="calculateTotal()">
          <?php echo $options; ?>
   </select>

  <select class="myport" id="myport" onChange="calculateTotal()">
  <option class="price" value=" ">Price</option>
  </select>

I have been trying to do this but so far no luck. I would appreciate your help. Thanks

Upvotes: 0

Views: 36

Answers (1)

Naim Sulejmani
Naim Sulejmani

Reputation: 1120

$query = "SELECT * FROM `devicehotsheet`";
$options = "";
$result1 = mysql_query($query, $dbhandle);
while ($row1= mysql_fetch_array($result1)){
$options=$options."<option value='$row1[0]#$row1[1]'>$row1[0]</option>";
}

and the javascript code

function calculateTotal(){
 var selDev = document.getElementById('selectDevice').value;
 var price = selDev.split('#')[1];
 document.getElementById('myport').innerHTML='<option>'+price+'</option>'
}

Upvotes: 1

Related Questions