我正面临这个麻烦......
我有这个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)select中的选项。
我经常搜索以找出将<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; ?>;
..但是语法和使我不喜欢的原则存在许多问题。
答案 0 :(得分:1)
如果我理解正确的话,这是一个粗略的草图,说明你可以做些什么。
<?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>
希望这会有所帮助......