0

I'm trying to have my code add this 2 fields to a form (quantities and products) on a click of a button.

How do I format my script so the innerHTML can include php code? Or is there another way to add a select element via JS?

newdiv.innerHTML = "<input type='text' name='quantities[]'> <select name='products[]'>
                        <?php
                            $sql = mysqli_query($link, "SELECT name FROM inventory");
                            while ($row = $sql->fetch_assoc()){
                                echo "<option value=\"".$row['name']."\">" . $row['name'] . "</option>";
                            }
                        ?>
                    </select>";
1
  • 1
    Escape the JavaScript string. PHP runs separately (first) before any JavaScript is ran. Commented Feb 5, 2017 at 23:12

1 Answer 1

3

PHP code is interpreted by your server before the page is even sent to the browser. The server then sends a completed html page (no PHP code) across the internet to your browser, which then loads it and then executes the JavaScript in the browser. The PHP interpreter would have no way of reading what the JavaScript changes on the finished page.

You might want to look into making an Ajax call to a PHP page, your PHP page can then contain the sql query and return data the JavaScript can use to add the options.

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

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.