【发布时间】:2011-09-28 10:26:20
【问题描述】:
我有一个发送消息表单,其中包含由 PHP 查询填充到数据库的收件人列表。我希望能够通过向表单动态添加选择框来包含多个收件人,具体取决于他们希望向多少人发送消息。
我不太了解 javascript,我一直在研究网络,以了解如何动态地将选择框添加到带有 PHP 填充选项的表单中,但我似乎唯一能找到的是如何根据在前一个选择框中选择的内容来填充选项。
这是我的 Javascript:
<script type="text/javascript">
function add_recipient_field(){
var container=document.getElementById('addanother');
var to_field=document.createElement('select');
to_field.name='to[]';
to_field.type='file';
container.appendChild(to_field);
var br_field=document.createElement('br');
container.appendChild(br_field);
}
</script>
这是我的 PHP :
$tosql = "SELECT UserID, FirstName, LastName FROM users WHERE Active = 'yes' ORDER BY LastName ASC";
$query = mysqli_query($dbc, $tosql) or die("Error: ".mysqli_error($dbc));
$tobox .= "<option value=\"None\">None</option>\n";
while ($row = mysqli_fetch_array($query)) {
if ($row['UserID'] == $to) {
$tobox .= "<option value=\"$row[UserID]\" selected=\"selected\">$row[LastName], $row[FirstName]</option>";
} else {
$tobox .= "<option value=\"$row[UserID]\">$row[LastName], $row[FirstName]</option>";
}
}
这是我的 HTML:
<div id="addanother"><li><label for="to">To: </label><select name="to[]" id="to">$tobox</select><a href="javascript:void(0);" onClick="add_recipient_field();">add more</a>$toerror</li></div>
当我单击“添加更多”链接时,它会添加另一个选择框,但是该框中没有选项,因此它显示为空。我知道我错过了一些相当简单的东西,但如果有人可以帮助或指出我的教程方向,我将非常感激。
【问题讨论】:
标签: php javascript