如何插入PHP';生成';选项(html选择)


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

我遇到了这个麻烦。。

我有这个JavaScript代码:

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> ";  

这个PHP代码:

$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>
";

每次点击提交时,我都用它来"生成"新的(html)输入。

但我不仅需要生成那些(html)输入,还需要生成(html)select,它处理数据库中的值,并将其显示为(html)selection中的选项。

我搜索了很多,想找到将从<select ..</select>的部分"插入"到newdiv.innerHTML变量的方法,但没有成功。我发现一些提示,我应该在(html)select中"解析"PHP代码,然后创建变量$no1 = mysqli_query($db, $goods); $no2 = while ($arr = mysqli_fetch_assoc($no1)...。。。最后只说JavaScript newdiv.innerHTML = <?php echo $no5; ?>;。。但是语法和原则上有很多问题,这让我很沮丧

你能帮我吗?)

如果我理解正确的话,下面是你可以做些什么的粗略草图。

<?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>

希望这能帮助。。。