Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to insert PHP 'generated' options (html select) dynamically using JavaScript?
    primarykey
    data
    text
    <p>I'm facing this trouble.. </p> <p>I have this JavaScript code:</p> <pre><code>echo "&lt;script type=\"text/javascript\"&gt; var counter = 1; function addInput(divName){ var newdiv = document.createElement('div'); newdiv.innerHTML = \"Entry \" + (counter + 1) + \" &lt;input type='text' name='myInputs[]'&gt;\"; document.getElementById(divName).appendChild(newdiv); counter++; } &lt;/script&gt; "; </code></pre> <p>and this PHP code:</p> <pre><code>$produkty="SELECT * FROM goods ORDER BY name"; if (isset($_POST['order'])) { $name = $_POST['myInputs']; foreach( $name as $key =&gt; $n ) { print $n." thank you\n &lt;br /&gt;"; } } echo " &lt;fieldset&gt;&lt;form method='post'&gt; &lt;div id='dynamicInput'&gt; &lt;select name='idp[]'&gt;"; $vys = mysqli_query($db, $goods); while ($arr = mysqli_fetch_assoc($vys)) { echo "&lt;option value='".$arr['id_good']."'&gt;".$arr['name']."&lt;/option&gt;"; } echo " &lt;/select&gt; &lt;br /&gt; Entry 1 &lt;input type='text' name='myInputs[]''&gt;&lt;br /&gt; &lt;/div&gt; &lt;input type='button' value='Add another text input' onClick=\"addInput('dynamicInput');\"&gt;&lt;br /&gt; &lt;input type='submit' name='order'&gt; &lt;/form&gt;&lt;/fieldset&gt; "; </code></pre> <p>and I use it to "generate" new (html) input everytime submit is clicked.</p> <p>But I need to generate not only those (html) inputs, but also that (html) select, which processes the values from the database and show it as options in that (html) select.</p> <p>I searched a lot to find out the way to "insert" the part from <code>&lt;select ..</code> to <code>&lt;/select&gt;</code> to the newdiv.innerHTML variable, but it wasn't succesful. I find some hints that I should "parse" the PHP code in (html) select and then create variable <code>$no1 = mysqli_query($db, $goods);</code> <code>$no2 = while ($arr = mysqli_fetch_assoc($no1)...</code> ... and in the end just say JavaScript <code>newdiv.innerHTML = &lt;?php echo $no5; ?&gt;;</code> .. but there were many problems with the syntax and with the principles that discouraged me.</p> <p>Can you help me please? ;)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload