从mysql数据中填充combobox

时间:2017-06-09 14:39:04

标签: javascript php mysql sql

我有3个组合框,请参阅以下代码



<select name="option1" id="option1" onchange="populate(this.id,'option2','option3','option4')">
    <option value=""></option>
    <option value="1">public</option>
    <option value="2">private</option>
</select><br>					
<select name="option2" id="option2" onchange="populate('option1',this.id,'option3','option4')">
    <option value=""></option>
    <option value="1">primary</option>
    <option value="2">secondary</option>
</select><br>				
<select name="option3" id="option3" onchange="populate('option1','option2',this.id,'option4')">
    <option value=""></option>
    <option value="1">office1</option>
    <option value="2">office2</option>
    <option value="3">office3</option>
    <option value="4">office4</option>
</select><br>
&#13;
&#13;
&#13;

我需要使用前3个组合框中的参数从mysql表中获取值,以填充最后一个组合框。所以这是我的SQL查询:

$sql = "SELECT * FROM `schools` WHERE `kind_of_education` LIKE '.$option1.' `AND `class_type` LIKE '.$option2.' AND `office` LIKE '.$option3.";`

我希望像这段代码一样:

&#13;
&#13;
<!DOCTYPE html>
<html>
<head>
<script>
function populate(s1,s2){
	var s1 = document.getElementById(s1);
	var s2 = document.getElementById(s2);
	s2.innerHTML = "";
	if(s1.value == "Chevy"){
		var optionArray = ["|","camaro|Camaro","corvette|Corvette","impala|Impala"];
	} else if(s1.value == "Dodge"){
		var optionArray = ["|","avenger|Avenger","challenger|Challenger","charger|Charger"];
	} else if(s1.value == "Ford"){
		var optionArray = ["|","mustang|Mustang","shelby|Shelby"];
	}
	for(var option in optionArray){
		var pair = optionArray[option].split("|");
		var newOption = document.createElement("option");
		newOption.value = pair[0];
		newOption.innerHTML = pair[1];
		s2.options.add(newOption);
	}
}
</script>
</head>
<body>
<h2>Choose Your Car</h2>
<hr />
Choose Car Make:
<select id="slct1" name="slct1" onchange="populate(this.id,'slct2')">
  <option value=""></option>
  <option value="Chevy">Chevy</option>
  <option value="Dodge">Dodge</option>
  <option value="Ford">Ford</option>
</select>
<hr />
Choose Car Model:
<select id="slct2" name="slct2"></select>
<hr />
</body>
</html>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

你必须在php中使用你的$ sql变量来调用你的数据库,然后代替列出平面选项,在你的查询结果上做一个foreach循环来填充选项的值

答案 1 :(得分:0)

我试过这段代码,但它有许多不起之处:

<script>
            function populate(op1,op2,op3,op4){
                    var op1 = document.getElementById(op1).value.toString();
                    var op2 = document.getElementById(op2).value.toString();
                    var op3 = document.getElementById(op3).value.toString();
                    var op4 = document.getElementById(op4).value.toString();
                    op4.innerHTML = "";
                       <?php
                         $op1="<script>op1.value.toString();</script>";
                         $op2="<script>op2</script>";
                         $op3="<script>op3</script>";
                         
                         $conn = mysql_connect('localhost','yaser95','123456789');

                         mysql_select_db('students',$conn);

                         $query = "SELECT `school_id`, `school_name` FROM `schools` WHERE `kind_of_education`=$op1 and `class_type`=1 and `office`=1 ORDER BY `school_name`";

                         $sSQL= 'SET CHARACTER SET utf8';
                         $result = mysql_query($sSQL,$conn) 	or die ('Can\'t charset in DataBase');

                         $result = mysql_query($query,$conn);
                         $selectbox='<select name=\'schools\'>';

                         while ($row = mysql_fetch_assoc($result)) {
                             "<script>
                             var newOption = document.createElement("option");
                             newOption.value =" $row['school_id']
                             "newOption.innerHTML =" $row['school_name']
                             "s2.options.add(newOption);
                           </script>"  
                          }
                       ?>    
            }
        </script>