In this code i m trying to populate dropdown list, when the variable $place
is available. Made an ajax call to get the data from the listplace.php file which is encoded in json format
Problem is the dropdown list not populating from the php file done through ajax call. But when i echo $_GET['place'];
it works and also the listplace.php also contains data but still not the dropdown populating. Please help!!
listplace.php output
{"option":["HBD T-Shirt"]}
Php Code
<?php
echo $_GET['place'];
?>
<select id="name">
<option selected disabled>Please select</option>
</select>
<?php if (isset($_GET['place']) && $_GET['place'] != '') { ?>
<script>
$.ajax({
type: "POST",
data: {place: '<?= $_GET['place'] ?>'},
url: 'listplace.php',
dataType: 'json',
success: function (json) {
if (json.option.length) {
var $el = $("#name");
$el.empty(); // remove old options
for (var i = 0; i < json.option.length; i++) {
$el.append($('<option>',
{
value: json.option[i],
text: json.option[i]
}));
}
}else {
alert('No data found!');
}
}
});
</script>
<?php } ?>