我的代码有问题。 像这样的情况: 我有一个下拉列表,如果选择“个人”,则会出现新的下拉列表,其中包含从数据库查询中检索到的数据;如果选择“公开”,则该下拉列表将消失。 这样的HTML代码:
<select name="use" class="dropdown" id="sender" onChange='changeSend()'> <option value=1>Public</option> <option value=0>Personal</option> </select> <div id='send2'></div>
查询如下:
<?php $query = mysql_query("select * from data where id_user = '$id_user' order by date asc"); $i = 0; $id = array(); $name = array(); while($data = mysql_fetch_array($query)){ //id from result database query $id[$i] = $data['id']; //name from result database query $name[$i] = $data['name']; $i++; } ?>
像这样的JavaScript代码:
function changeSend() { var selectBox = document.getElementById("sender"); var selectedValue = selectBox.options[selectBox.selectedIndex].value; if (selectedValue==0) { $('#send2').html("<select class='dropdown'><option value='-id from result database-'>-name from result database query-</option></select>"); } else { $('#send2').html(''); } }
我不知道如何将值/结果发送($id[0],$name[0],$id[1],$name[1], etc..)到javascript代码(选择选项中的值和名称)。
($id[0],$name[0],$id[1],$name[1], etc..)
在javascript中,您必须ajax call对您的php文件进行操作:
ajax call
var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("send2").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","yourFile.php",true); xmlhttp.send();
并且在您的php文件中,您必须以echoJSON格式存储数据:
echo
echo json_encode(array('id'=>$id,'name'=>$name));
*在您的情况下, *UPDATE 使用以下代码:(未测试)
php代码:
<?php $query = mysql_query("select * from data where id_user = '$id_user' order by date asc"); $i = 0; $options = array(); while($data = mysql_fetch_array($query)){ $options[$data['id']] = $data['name']; } echo json_encode($options); ?>
JavaScript代码:
var xmlhttp; if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else{// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function(){ if (xmlhttp.readyState==4 && xmlhttp.status==200){ var response = JSON.parse(xmlhttp.responseText); var select = '<select class='dropdown'>'; for( var index in response ){ select = select + "<option value='"+ index +"'>"+response[index]+"</option>"; } select += "</select>"; document.getElementById("send2").innerHTML= select; } } function changeSend() { var selectBox = document.getElementById("sender"); var selectedValue = selectBox.options[selectBox.selectedIndex].value; if (selectedValue==0) { xmlhttp.open("GET","yourFile.php",true); xmlhttp.send(); } else { $('#send2').html(''); } }
使用jQuery
function changeSend() { var selectBox = document.getElementById("sender"); var selectedValue = selectBox.options[selectBox.selectedIndex].value; if (selectedValue==0) { $.get("yourFile.php", function(data){ var response = JSON.parse(data); var select = '<select class='dropdown'>'; for( var index in response ){ select = select + "<option value='"+ index +"'>"+response[index]+"</option>"; } select += "</select>"; $("#send2").html(select); }); } else { $('#send2').html(''); } }