Beginner Beginner - 1 year ago 51
Ajax Question

loading data from ajax into html label and listbox not working

I am making an ajax to call data from database with php and i want to load the result in two inputs(label and listbox) in html page,the problem is that it show the value in the label

but it is not showing anything in the listbox
Here is the ajax

dataType: 'json',
'Order_ID': Order_ID,
success: function(data)
error: function(jqXHR, textStatus, errorThrown) {
console.log('ERROR', textStatus, errorThrown);


And this is the php code:

$stmt1 ="SELECT distinct Farm_id FROM ordersfarmers WHERE Order_ID='".$_POST["Order_ID"]."'AND Reply='1'";

foreach ($conn->query($stmt1) as $row)
$json = array("FarmerCount" => $res,
"FarmersID" => $row['Farm_id']
echo json_encode($json);

and the html part

<select name="FarmersID" id="FarmersID" multiple style="width:280px;height:110px;" onclick="showonmap()">
<label id="FrmCount"></label>

Answer Source

With code $("#FarmersID").html(data.FarmersID); you basiclly creating something like:

<select name="FarmersID" id="FarmersID" multiple style="width:280px;height:110px;" onclick="showonmap()">
1 <!-- your id -->

That's not a proper select format, what you need is create option inside html. For this example you can do:

$("#FarmersID").append('<option value="' + data.FarmerID + '">' + data.FarmerID + '</option>');

But you will always end up with one element. You should return an array (you are not doing that at the moment) or you can build proper html in your php and return that to your javascrip.


foreach ($conn->query($stmt1) as $row) 
    $farmesIds[$row['Farm_id']] = $row['Farm_id'];
$json = array(
    "FarmerCount" => $res,
    "FarmersID" => $farmesIds
echo json_encode($json);

And in javascript

$.each(data.FarmersID, function(id, value) {
    $("#FarmersID").append('<option value="' + id + '">' + value + '</option>');

You can pass something else as value (label or smth?) and keep ID as a key.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download