Javascript JS函数创建一个下拉列表,其中填充了DB onclick中的名称?
我需要帮助写这段小代码。我需要一个函数,它可以创建一个下拉菜单,当单击按钮时,该菜单将填充数据库中的名称。下拉列表中的名称最终将提交到数据库中的另一个表中,因此我需要知道如何在下拉列表中将名称设置为“vales”。实施这些问题的最佳方式是什么?我已经指出了我认为事情应该走向何方 Javascript:Javascript JS函数创建一个下拉列表,其中填充了DB onclick中的名称?,javascript,php,jquery,html,Javascript,Php,Jquery,Html,我需要帮助写这段小代码。我需要一个函数,它可以创建一个下拉菜单,当单击按钮时,该菜单将填充数据库中的名称。下拉列表中的名称最终将提交到数据库中的另一个表中,因此我需要知道如何在下拉列表中将名称设置为“vales”。实施这些问题的最佳方式是什么?我已经指出了我认为事情应该走向何方 Javascript: var room = 1; function ReviewForm() { room++; var objTo = document.getElementById('Review
var room = 1;
function ReviewForm() {
room++;
var objTo = document.getElementById('ReviewForm')
var divtest = document.createElement("div");
divtest.setAttribute("class", "form-group removeclass" + room);
var rdiv = 'removeclass' + room;
divtest.innerHTML =
'<hr>' +
'<h2>Student:</h2>' +
'<select>' +
//Instead of these three names, I need to use the DB's info go generate the text and values
'<option value="Adam">Adam</option> ' +
'<option value="Bob">Bob</option> ' +
'<option value="Cat">Cat</option>' +
'</select></h2>' +
objTo.appendChild(divtest)
var房间=1;
函数ReviewForm(){
房间++;
var objTo=document.getElementById('ReviewForm')
var divtest=document.createElement(“div”);
setAttribute(“类”、“窗体组removeclass”+房间);
var rdiv='removeclass'+房间;
divtest.innerHTML=
“
”+
“学生:”+
'' +
//我需要使用DB的info来生成文本和值,而不是这三个名称
“亚当”+
“鲍勃”+
“猫”+
'' +
对象追加子对象(divtest)
PHP:
您尝试过jquery ajax吗?您会给元素一个id,在后台生成选项,并将它们作为数据从ajax回调函数加载到select元素中。或者更多帮助?您尝试过jquery ajax吗?您会给元素一个id,在后台生成选项,并将它们作为数据从中加载到select元素中ajax回调函数。或者更多帮助?
<?php
$NumberOfStudents = "";
// $NumberOfStudents=$_GET['SelectedStudents'];
$hostname = "host";
$username = "user";
$password = "pass";
$databaseName = "_db";
$conn ;
$connect = mysqli_connect($hostname, $username, $password, $databaseName);
/*if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} */
//BELOW IS THE QUERY FROM THE DB THAT I AM TRYING TO GET TO REPRINT EACH TIME THE FUNCTION IS CALLED
$query = "SELECT concat (FirstName,' ', LastName) as StudentNames FROM `Student`";
$result = mysqli_query($connect, $query);
?>
<body>
<!-- Wrapper -->
<div id="wrapper">
<!-- Main -->
<div id="main">
<div class="inner">
<!-- Content -->
<section>
<!-- Form -->
<form method="post" action="#">
<div class="row uniform">
<!-- Break -->
<div class="12u$">
<div style="height:550px;border:1px solid #ccc;font:16px/26px Georgia, Garamond, Serif;overflow:auto;"><!-- container for reviews-->
<div id="ReviewForm">
<hr>
<h2>
Student:
<select>
<!-- It is hardcoded into the page here, which is fine, but I would rather it generate when the page is loaded, and then can
be called again each time a new dropdown is needed. -->
<?php while($row1 = mysqli_fetch_array($result)):;?>
<option value="<?php echo $row1['StudentNames'];?>"><?php echo $row1['StudentNames'];?></option>
<?php endwhile;?>
</select>
</h2>
</div>
</div>
</form>
<input type="hidden" name="count" value="1" />
<div class="control-group" id="fields">
<label class="control-label" for="field1"></label>
<div class="controls" id="profs">
<form class="input-group-btn">
<div>
<!--Here is the button that calls the function for a new dropdown menu-->
<button class="btn btn-success" type="button" onclick="ReviewForm();">Add another student</button>
</div>
</form>
<br>
<br>
</div>
</div>
</section>
</div>
</div>
</div>
<!-- Scripts -->
<script src="assets/js/jquery.min.js"></script>
<script src="assets/js/skel.min.js"></script>
<script src="assets/js/util.js"></script>
<!--[if lte IE 8]><script src="assets/js/ie/respond.min.js"></script><![endif]-->
<script src="assets/js/main.js"></script>
</body>