Hello.
I have tired too much ways to overcome this issue but can't find a way yet.
I am making a user defined function, when click a "And new row", This function will be called and a new row will be added. There in first cell, a dropdown menu will be appper, where options will be collected from sql table doctor_t. All going well, but menu options are missing. Please help..

The code is here

    var element = document.createElement("select");//create select element
    element.setAttribute("name", "text");
    var option = document.createElement("option");//create option element
    <?php
    //Server= localhost, Username= root, pasword = "there is no password" and db name = bsc_db.
    mysql_connect('localhost', 'root', '') or die('Connection could not be established');
    mysql_select_db('bsc_db') or die('Database is not present');
    $q = mysql_query("SELECT * FROM doctor_t");
    $options = '';
    while($r = mysql_fetch_array($q))
    {
    $options .= '<option value="'.$r['id'].'">'.$r['dname'].'</option>';
    }
    ?>
    option.text = '<?php echo $options;?>';//option text attribute
    option.value= "text";
    //option.value = "name";//option value attribute
    try
    {
    element.add(option, null); //Standard
    }catch(error) {
    element.add(option); // IE only
    }
    cell1.appendChild(element);

there is error in line 20 and 22.The variable name must be options that you have created above.

Why are you using these two lines.Remove these two lines.

 option.text = '<?php echo $options;?>';//option text attribute
    option.value= "text";

This might solve your problem as you are adding option which is a group and assigning value to it that is an error.

So your code will be :-

$options = '';
while($r = mysql_fetch_array($q))
{
$options .= '<option value="'.$r['id'].'">'.$r['dname'].'</option>';
}
?>

try
{
element.add(options, null); //Standard
}catch(error) {
element.add(options); // IE only
}
cell1.appendChild(element);

Nope. I am still not geetting point. Their is still an error I am making. Its still not working. I have tried the code you mentioned above.

When I click the "Insert New Row" button, A line is inserted in the new row in all 4 cells.

you need to add each option-element to the select-element separately.
So put the element.add insde the while loop:

    var element = document.createElement("select");//create select element
    element.setAttribute("name", "text");
    var option = document.createElement("option");//create option element
    <?php
    //Server= localhost, Username= root, pasword = "there is no password" and db name = bsc_db.
    mysql_connect('localhost', 'root', '') or die('Connection could not be established');
    mysql_select_db('bsc_db') or die('Database is not present');
    $q = mysql_query("SELECT * FROM doctor_t");
    $options = '';
    while($r = mysql_fetch_array($q))
        {
        $options .= '<option value="'.$r['id'].'">'.$r['dname'].'</option>';

        ?>
        option.text = '<?php echo $options;?>';//option text attribute
        option.value= "text";
        //option.value = "name";//option value attribute
        try {
            element.add(option, null); //Standard
            }
        catch(error) {
            element.add(option); // IE only
            }

   <?php  } // end while ?>  
    cell1.appendChild(element);
Member Avatar for diafol

Urgh! Sorry, no offence, but mixing all that php in the js gives me the shivers. How about separating out the php (e.g. above the DTD or in an include file) and just leaving the echo variable? Your js will be a lot cleaner and it'll be easier to maintain.

@pzuurveen.
Thanks for suggession. I have tried your instruction but its taking these options like string. I am puzzled where is the mistake....

Image shows the mess.Click HereWhen I click on the "New Row" Button. So get the stuff just shown in the attached image...

This should be better

    var element = document.createElement("select");//create select element
    element.setAttribute("name", "text");
    var option = document.createElement("option");//create option element
    <?php
    //Server= localhost, Username= root, pasword = "there is no password" and db name = bsc_db.
    mysql_connect('localhost', 'root', '') or die('Connection could not be established');
    mysql_select_db('bsc_db') or die('Database is not present');
    $q = mysql_query("SELECT * FROM doctor_t");

    while($r = mysql_fetch_array($q))
        {
        ?>
        option.text = '<?php echo $r['dname'];?>';//option text attribute
        option.value= "<?php echo $r['id']; ?>";
        try {
            element.add(option, null); //Standard
            }
        catch(error) {
            element.add(option); // IE only
            }
    <?php } // end while ?>
    cell1.appendChild(element);
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.