动态更新JavaScript和PHP中的select元素选项

时间:2016-02-24 05:29:57

标签: javascript php drop-down-menu html-select

我在填充下拉列表时遇到问题。

我有一个用户选择分支的下拉列表,以及显示相关选项的第二个下拉列表。

HTML

<select id="first-choice" onchange="leaveChange()">
    <option selected value="base">Please Select</option>
    <option value="CSE">CSE</option>
    <option value="ECE">ECE</option>
    <option value="EEE">EEE</option>
    <option value="MECH">MECH</option>
</select>
<br>
<select id="second-choice">
    <option>Please choose from above</option>
</select>

JS

function leaveChange(){
    //what to insert here;
}

PHP

$branch=$_GET['branch'];
$username = "jaggu";
$password = "8374";
$hostname = "localhost";

$dbhandle = mysqli_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
$selected = mysqli_select_db($dbhandle,"test") or die("Could not select examples");

$query = "SELECT * FROM `register` WHERE classid LIKE '%".$branch."%'";

$result = mysqli_query($dbhandle,$query);
    $row = mysqli_fetch_object($result);
    $query1="SELECT * FROM `examdup` WHERE `classid` LIKE '%".$branch."%'";
$result1= mysqli_query($dbhandle,$query1);  

while ($row1= mysqli_fetch_object($result1)) {
    echo '<option value="'.$row1->month_year.'">'. $row1->title.'</option>';
}

1 个答案:

答案 0 :(得分:1)

以下是您的问题http://www.w3schools.com/php/php_ajax_database.asp

的确切答案

如果将此示例集成到您的代码中,则应该是:

<强> HTML

php 1.php -xmycommand -d"my option"

<强> JS

var_dump(getopt('x:d:'));

您应该将文件“test.php”的名称替换为此行 xmlhttp.open(“GET”,“test.php?branch =”+ branch,true); < / p>