简体   繁体   中英

How to insert PHP 'generated' options (html select) dynamically using JavaScript?

I'm facing this trouble..

I have this JavaScript code:

echo "<script type=\"text/javascript\">
    var counter = 1;
    function addInput(divName){
              var newdiv = document.createElement('div');
              newdiv.innerHTML = \"Entry \" + (counter + 1) + \" <input type='text' name='myInputs[]'>\";
              document.getElementById(divName).appendChild(newdiv);
              counter++;
    }
</script> ";  

and this PHP code:

$produkty="SELECT * FROM goods ORDER BY name";

    if (isset($_POST['order'])) {
        $name = $_POST['myInputs'];

        foreach( $name as $key => $n ) {
          print $n." thank you\n <br />";
        }
    }        
echo "
        <fieldset><form method='post'>            
              <div id='dynamicInput'>
                       <select name='idp[]'>";
                           $vys = mysqli_query($db, $goods);
                           while ($arr = mysqli_fetch_assoc($vys)) {
                               echo "<option value='".$arr['id_good']."'>".$arr['name']."</option>";               
                              }
                              echo "
                       </select> <br />
                       Entry 1 <input type='text' name='myInputs[]''><br />
               </div>
               <input type='button' value='Add another text input' onClick=\"addInput('dynamicInput');\"><br />
               <input type='submit' name='order'>
        </form></fieldset>
";

and I use it to "generate" new (html) input everytime submit is clicked.

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.

I searched a lot to find out the way to "insert" the part from <select .. to </select> 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 $no1 = mysqli_query($db, $goods); $no2 = while ($arr = mysqli_fetch_assoc($no1)... ... and in the end just say JavaScript newdiv.innerHTML = <?php echo $no5; ?>; .. but there were many problems with the syntax and with the principles that discouraged me.

Can you help me please? ;)

Here is a rough sketch of what you can do for this, if I am understanding you correctly.

<?php
$options = "";
$vys = mysqli_query($db, $goods);
while ($arr = mysqli_fetch_assoc($vys)) {
   $options .= "<option value='".$arr['id_good']."'>".$arr['name']."</option>";               
 }

echo <<< _html
<form method="post">            
      <div id="dynamicInput">
            <div>
                <select name=idp[]>
                    $options
                </select> <br />
                Entry 1 <input type="text" name=myInputs[]><br />
            </div>
       </div>
       <input type="button" value="Add another text input" onClick="addInput('dynamicInput');"><br />
       <input type="submit" name="order">
</form>

_html;

?>

<script type="text/javascript">
var counter = 1;
function addInput(divName){
          var newdiv = document.createElement('div');
          newdiv.innerHTML = "<select name=idp[]><?php echo $options; ?></select> Entry " + (counter + 1) + " <input type='text' name='myInputs[]'>";
          document.getElementById(divName).appendChild(newdiv);
          counter++;
}
</script>

Hope this helps...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM