0

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

3 Answers 3

1

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>'; 
});
Sign up to request clarification or add additional context in comments.

Comments

0

i is the customer ID!

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

Comments

0

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);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.