I checked the boards and Googled for this code but couldn’t find anything. Any help would be greatly appreciated.
I have worked through a tutorial that queries a database for a car model based on the car make. In the code, the car make is hardcoded to the select while the car model is dynamic. Here’s the first part of the code:
<?php
require_once("Connections/xxx.php"); // database connection
$make = $_POST['make'];
if ($make){
$query= sprintf("SELECT * FROM car_model where car_model_id='$make'");
$result = @mysql_query($query);
$rowModel = mysql_fetch_array($result);
}
?>
Here’s the select hard code:
<select name="make" onChange="document.forms[0].submit()">
<option value="">Select Make</option>
<option value="1" <?php if(!(strcmp(1, $make))){echo "selected";}?>>Alfa Romeo</option>
<option value="2" <?php if(!(strcmp(2, $make))){echo "selected";}?>>Audi</option>
<option value="3" <?php if(!(strcmp(3, $make))){echo "selected";}?>>BMW</option>
<option value="4" <?php if(!(strcmp(4, $make))){echo "selected";}?>>Citroen</option>
And this is the dynamically populated select:
<select name="model">
<option value="">Select Model</option>
<?php do { ?>
<option value="<?php echo $rowModel['car_model_id']; ?>"><?php echo $rowModel['car_model']; ?></option>
<?php }while ($rowModel = mysql_fetch_array($result)); ?>
I would like to add an onChange function to this dynamically populated select field to echo result to a textfield and I have tried many combinations but haven’t been able to figure this out. Thank you
Since you didn’t like my jquery version, which I still feel is a better answer here the javascript way which I’m sure is not going to be supported in every browser and be very buggy (thus the reason most people use jquery)