I am using two dropdowns in a form for searching. The second dropdown value will change based on the selection of the first one. The ajax request goes but somehow not returning any value from the php script. Any suggestions for change in code?
search.php
<select name="shop_name" id="shop_name">
<option value="blank">Select one</option>
<?php
$searchItem = new dbhandler();
$shopData = $searchItem->all_data('food_shops');
$i = 0;
foreach($shopData as $sd) {
?>
<option value="<?php echo $shopData[$i]->r_id; ?>"><?php echo $shopData[$i]->name; ?></option>
<?php $i++; } ?>
</select>
script.js
<script type="text/javascript">
$(document).ready(function()
{
$("#shop_name").change(function()
{
var id=$(this).val();
var dataString = 'id=' + id;
$.ajax
({
type: "GET",
url: "dynamic_select.php",
data: dataString,
success: function(data)
{
$("#location").html(data);
}
});
});
});
dynamic_select.php
<?php
if($_GET['id']) {
$crs = $_GET['id'];
?>
<select name="location" id="location">
<?php
$searchItem = new dbhandler();
$searchItem->find('food_shops' , 'r_id', $crs);
$data = $searchItem->data();
$j = 0;
foreach($data as $d) {
?>
<option value="<?php echo $data[$j]->address; ?>"><?php echo $data[$j]->address; ?></option>
</select>
<?php $j++; } } ?>
The all_data() queries SELECT * FROM table_name
and find() queries SELECT * FROM table_name WHERE r_id = $crs

urloption for your ajax call.$("#location").replaceWith(data);