Hey everyone..

I am relatively new to PHP and JS and I need help.

I need to populate a dropdown list from a db(mysql). I can do it just with PHP.
The problem arises when I add another of the same dropdown to my form using javascript.
I need the combo box to show the same values that come from the MySQL DB.

Is there any way to accomplish this? Waiting on a reply....

Thanks
V

Recommended Answers

All 2 Replies

Here in Daniweb people help if you show some efforts.
Show what you have done and people will do the rest.

Here in Daniweb people help if you show some efforts.
Show what you have done and people will do the rest.

Well I am sorry I did not give enough info.
Here goes....

This is the code where I a create a dropdown with options fetched from the DB using option_fetch()

<select name="options[]" id="options1">
    <option value="0">-- For Options --</option>
         <?php 
	    include_once "test1.php";
	    option_fetch();
	 ?>
</select>

test1.php

function option_fetch() 
{
  include_once "connect.php";
  $con = connectDB(); //part of connect.php this works fine... 
  $query = "Select * from options"; 								
  $result = mysql_query($query) or die("SQL Error -->". mysql_error());
  while ($row = mysql_fetch_array($result))
	print '<option value="$row["options"]">'.$row["options"].'</option>\n';
}

Now I have a button that adds another row with the same field into the opt div.

<input type="button" name="addrow" id="addrow" value="Add another Row" onclick="javascript: return addRow('opt');">

The function addRow(divName) creates another row with the same field. But, it creates an empty dropdown and doesnt fetch from the DB.

addRow(divName)
{
    var divN = divName;
	var cnt = 1;
	alert(divN);
	var newdiv = document.createElement('div');
	newdiv.id = 'newrow'+cnt;
	document.getElementById(divN).appendChild(newdiv);
	alert(newdiv.id);
	document.getElementById(newdiv.id).innerHTML = 
        '<div id ="row_+cnt">'+
	   '<table border="0">'+
	      '<tr>'+
		'<td width="20%" align="left" style="color:black; background-color:#FFF8C6;">'+
	           '<select name="option1[]" id="option1[]" onclick="">'+
		       '<option value="0">-- For Options --</option>'+
                      //Need to add code to retrieve value from the DB as shown above
                   '</select>'+
		'</td>'+
              '</tr>'+
	  '</table>'+
	'</div>';
     cnt++;
}

I am stuck at this point. Any help will be appreciated. Thanks a ton.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.