Reputation: 37
I need to select an id depending on another id. I made by myself something but it is not working, so I am posting the CODE, maybe i have something wrong in it. Select the first option a vehicle, and in the second option select a model of the vehicle that I selected. Thanks!
//The config file
<?php
$host = 'localhost';
$utilizator = 'stud';
$parola = 'stud';
$numebd = 'autodealer';
$conn = mysql_connect($host, $utilizator, $parola);
if (!$conn) { echo '<h4>Connected!</h4>'; }
if (!mysql_select_db($numebd, $conn)) {
echo '<h4>Couldnt connect database : '. mysql_errno(). ' : '. mysql_error().'</h4>';
}
mysql_set_charset('utf8', $conn);
?>
//The HTML code
<h2><strong>Vehicle</strong> data</h2>
<div class="select_wrapper">
<form action="" method="post" name="anunt" id="f_anunt">
<label><span>* </span><strong>Manufacturer:</strong></label>
<select class="select_5" name="marca" id="marca">
<?php
$result = mysql_query("SELECT id_marca, denumire FROM marci ORDER BY ordine");
while($row = mysql_fetch_array($result)) {
echo ("<option value='".$row['id_marca']."'>".$row['denumire']."</option>");
}
?>
</select>
</div>
<div class="select_wrapper">
<label><span>* </span><strong>Model: </strong></label>
<select class="select_5" name="model" id="model">
<?php
$result = mysql_query("SELECT id_model, denumire FROM modele WHERE id_marca=1");
while ($row = mysql_fetch_array($result)) {
echo ("<option value='".$row['id_model']."'>".$row['denumire']."</option>");
}
?>
</select>
</div>
//The PHP file
<?php
include_once("config.php");
$id_marca = $_POST['id_marca'];
$sir_sql="SELECT id_model, denumire FROM modele WHERE id_marca=$id_marca ";
if ($conn())
{
$result=mysql_query($sir_sql,$conn);
while ($row = mysql_fetch_array($result))
echo ("<option value='".$row['id_model']."'>".$row['denumire']."</option>");
}
else
die('Comanda incorecta:<br> ' . mysql_error());
?>
//The Javascript code
$("#marca").change(get_model);
function get_model(e)
{
$.post("ajax_get_model.php",{id_marca:$(this).val()},function(data,status)
{
$("#model").html(data); // pun modelele
});
}
Upvotes: 0
Views: 10672
Reputation:
There are several post on SO
about this topic. Please, read these topics first, hope these will help-
Finally, you can check the tutorial here to populate dropdown based on dropdown selection...
Upvotes: 1