[英]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)選擇,該輸入處理數據庫中的值並將其顯示為該(html)選擇中的選項。
我進行了很多搜索,以找出將部分從<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>
希望這可以幫助...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.