2

I created a javascript dropdown using this code:

var cell3 = row.insertCell(2);
var element3 = document.createElement("select");
element3.name = "prddrop[]";
element3.id = "prddrop[]";
cell3.appendChild(element3);

My problem is to populate that combobox with data coming from a PHP associative array.

<?php
if(isset($prods) && count($prods) > 0)
{ 
foreach($prods as $key=>$p)
    {
$productID= $p['pid'];
$productName=$p['pname'];
   }
}
?>

Please Help me with this.

2 Answers 2

1

You can use Jquery with something like this if that dosent bother you,

          jQuery.each(data, function(key, value) {
                jQuery('select[name="' + populatedElement + '"]')
                        .append(jQuery("<option></option>")
                        .attr("value", key)
                        .text(value));
            });

It will dynamcially add values to your select box.

Sign up to request clarification or add additional context in comments.

3 Comments

I did not understand, can you make a fiddle for me?
well i havent tested it on fiddle exactly but data will be array and PopulatedElement will be name of your select box where as key and value are normal array (key=>value), i have done same code on my local , following my perspective
Thanks I used json to get the data from my array and the rest follows. Thank you for your help.
0

Use AJAX, to load it on the fly. Or you could always echo your PHP out directly into your script.

http://www.w3schools.com/ajax/

http://api.jquery.com/jQuery.ajax/

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.