Umpong_1947676
Umpong_1947676

Reputation: 111

Populate Dynamically Created Dropdown with Options from PHP array using javascript

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.

Upvotes: 2

Views: 1206

Answers (2)

noobie-php
noobie-php

Reputation: 7243

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.

Upvotes: 1

Jonathon
Jonathon

Reputation: 16333

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/

Upvotes: 0

Related Questions