0

I was trying to make a dropdown menu with dynamic contents from php elements in javascript, but when I click on the button add item, nothing happened. the elements from php is stored in $item[]

<script>
function addInput()
{
    var table = document.getElementById("item");
    var row = table.insertRow(0);
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);
    var cell3 = row.insertCell(2);
    var cell4 = row.insertCell(3);
    cell1.innerHTML = "<select name="i1[]">
            <?php 
                echo "<option selected disabled>Choose Item Code</option>";
                $i=0;
                while(!empty($item[$i]))
                {
                    echo "<option value=".$item[$i].">".$item[$i]."</option>";
                    $i++;
                }
             ?></select>";
    cell2.innerHTML = "<input type='text' name='q1[]' size=10>";
    cell3.innerHTML = "<input type='text' name='p1[]' size=10>";
}
</script>
5
  • 1
    Just pointing out there is an error in your line cell1.innerHTML = "<select name="i1[]">, you are using double quotes around "i1[]" when I think you probably mean to use 'i1[]'. Commented Mar 16, 2016 at 6:08
  • 1
    @ChrisTomich he should do it the other way around: use single quotes for all JS strings thus allowing you to use double quotes (the standard) for your HTML. @weeo it should be '<select name="i1[]"> and then at the end: </select>'; Commented Mar 16, 2016 at 6:11
  • can you show your html with item elements? Commented Mar 16, 2016 at 6:13
  • @ChrisTomich It doesn't work either... Commented May 1, 2016 at 1:24
  • @MatthewHerbst It doesn't work either... Commented May 1, 2016 at 1:25

3 Answers 3

1

Your should add "+" symbol after every line that you generated by PHP

function addInput()
{
        var table = document.getElementById("item");
        var row = table.insertRow(0);
        var cell1 = row.insertCell(0);
        var cell2 = row.insertCell(1);
        var cell3 = row.insertCell(2);
        var cell4 = row.insertCell(3);
        cell1.innerHTML = ""+
            "<select name='i1[]'>"+
                <?php 
                        echo "'<option selected disabled>Choose Item Code</option>'+";
                        $i=0;
                        while(!empty($item[$i]))
                        {
                                echo "'<option value=".$item[$i].">".$item[$i]."</option>'+";
                                $i++;
                        }
                 ?>
        "</select>";
        cell2.innerHTML = "<input type='text' name='q1[]' size=10>";
        cell3.innerHTML = "<input type='text' name='p1[]' size=10>";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! what happened when I don't have the +?
1

You need to quote the code you are echoing with PHP since it will be considered and executed as javascript code not as string quotes that shall be assigned to the innerHTML property.

Besides, you shall concatenate the options.

It is better to build your select with PHP variables where you add all the options to it and then assign it one shot to the cell innerHTML

function addInput()
{
    var table = document.getElementById("item");
    var row = table.insertRow(0);
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);
    var cell3 = row.insertCell(2);
    var cell4 = row.insertCell(3);

    <?php 
        $i=0;
        $select = "<select name='i1[]'";
        $options = "<option selected disabled>Choose Item Code</option>";
        while(!empty($item[$i]))
        {
           $options .= "<option value=".$item[$i].">".$item[$i]."</option>";
            $i++;
        }

        $select .= $options . "</select>";
     ?>

    cell1.innerHTML = "<?php echo $select;?>";
    cell2.innerHTML = "<input type='text' name='q1[]' size=10>";
    cell3.innerHTML = "<input type='text' name='p1[]' size=10>";
}

1 Comment

thank you! it works. Why it doesn't work using the simple "->' fix?
0

Use it like this:

<script>
function addInput()
{
    var table = document.getElementById("item");
    var row = table.insertRow(0);
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);
    var cell3 = row.insertCell(2);
    var cell4 = row.insertCell(3);
    cell1.innerHTML = '<select name="i1[]">
            <?php 
                echo "<option selected disabled>Choose Item Code</option>";
                $i=0;
                while(!empty($item[$i]))
                {
                    echo "<option value=".$item[$i].">".$item[$i]."</option>";
                    $i++;
                }
             ?></select>';
    cell2.innerHTML = "<input type='text' name='q1[]' size=10>";
    cell3.innerHTML = "<input type='text' name='p1[]' size=10>";
}
</script>

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.