Reputation: 586
How to pass php array value to jquery select box.
i tried like
$sql_customer = mysql_query("select * from tbl order by customer_name");
while($row_customer = mysql_fetch_array($sql_customer)){
$customer_arr[$row_customer['id']] = $row_customer['customer_name'];
}
And pass into jquery as json encoded value
var customerarray = ;
var seloption = '';
$.each(customerarray, function (i, elem) {
seloption += '<option value="'+customerarray[i]+'">'+customerarray[i]+'</option>';
});
How can i get table id in option value, now its getting customer name
Upvotes: 0
Views: 870
Reputation: 923
How about doing it without concatenation and quick workarounds?
$(customerarray).each(function(i, e) {
var o = new Option(e.customer_name, e.customer_id);
$('#select-tag').append(o);
});
Upvotes: 0
Reputation: 29922
i
is the customer ID!
var seloption = '';
$.each(customerarray, function (i, elem) {
seloption += '<option value="'
+ i + '">'
+ customerarray[i] + '</option>';
});
Upvotes: 0
Reputation: 8101
Just set i
as option value as it contains key of an array which is cutomer id.Try:
var seloption = '';
$.each(customerarray, function (i, elem) {
seloption += '<option value="'+i+'">'+customerarray[i]+'</option>';
});
Upvotes: 1