i have a problem with my dynamic select option. My problem is my select option didn't show anything, just show blank. I have tried many times and i am stuck
this is my html select option code
<select name="jumlahpesan" id="jumlahpesan" data-native-menu="false">
<option value="choose-one" data-placeholder="true">Choose one... </option>
</select>
and this is my ajax code to get value to fill in my select option
$.ajax({
url: host+'/skripsi3/phpmobile/cekjumlah.php',
data: { "id": getacara},
dataType: 'json',
success: function(data, status){
$.each(data, function(i,item){
$("#jumlahpesan").append('<option value="'+item.jumbros+'">"'+item.jumbros+'"</option>').trigger("create")
});
},
error: function(){
//output.text('There was an error loading the data.');
}
});
and for the last, this is my "cekjumlah.php"
<?php
session_start();
include "config.php";
$idacara=mysql_real_escape_string($_GET["id"]);
$arr = array();
$result=mysql_query("select jumlahpesan from acara where id_acara='$idacara'");
if (!empty($result))
{
while ($row=mysql_fetch_array($result))
{
$tempjum = $row['jumlahpesan'];
for($i=0;$i<$tempjum;$i++)
{
$fetchkategori[] = array
(
'jumbros' => $i,
);
}
}
}
mysql_close($con);
header('Content-Type:application/json');
echo json_encode($fetchkategori);
?>
i want to fill my select option from my looping in "cekjumlah.php" and call my php with my ajax. Thank you
this is my Ajax Response
[{"jumbros":0},{"jumbros":1},{"jumbros":2}]
I can't really help you with the PHP side of things, so if there is a problem there, I can't see it.
But you seem to be using .each() incorrectly, as each is meant to iterate over a jquery selection of elements and execute a funciton for each element, whereas your seem to be trying to use it as a foreach over data
I think you have to replace that $.each with
for item in data{
$("#jumlahpesan").append('<option value="'+item.jumbros+'">"'+item.jumbros+'"</option>').trigger("create")
}
(i'm assuming data is a JSON array, you may have to use JSON.parse(data) first judging by some of the comments on your post.