Jasir alwafaa
Jasir alwafaa

Reputation: 586

PHP array to jquery select box

How to pass php array value to jquery select box.

My Table enter image description here

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

Answers (3)

Nishant Ghodke
Nishant Ghodke

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

feeela
feeela

Reputation: 29922

i is the customer ID!

var seloption = '';
$.each(customerarray, function (i, elem) {
    seloption += '<option value="'
        + i + '">'
        + customerarray[i] + '</option>'; 
});

Upvotes: 0

Dhara Parmar
Dhara Parmar

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

Related Questions