从查询 sql 数据库获取数据到 javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23237047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 00:36:01  来源:igfitidea点击:

getting data from query sql database to javascript

javascriptphpjqueryhtmlsql

提问by user3193610

I have a problem with my code.
case like this:
I have a dropdown, if selected "personal" it appeared the new dropdown that contains the data that is retrieved from a database query, if selected "public", then the dropdown disappear.
HTML code like this:

我的代码有问题。
像这样的情况:
我有一个下拉列表,如果选择“个人”,它会出现包含从数据库查询中检索到的数据的新下拉列表,如果选择“公共”,则下拉列表消失。
像这样的 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>

Query like this:

像这样查询:

<?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 code like this:

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('');
   }
}

I dont know how to send value/result ($id[0],$name[0],$id[1],$name[1], etc..)to javascript code(value and name in select options).

我不知道如何将值/结果发送($id[0],$name[0],$id[1],$name[1], etc..)到 javascript 代码(选择选项中的值和名称)。

回答by TheGr8_Nik

In javascript you have to make an ajax callto your php file:

在 javascript 中,你必须ajax call对你的 php 文件做一个:

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();

And in your php file you have to echoyour data in JSON format:

在您的 php 文件中,您必须使用echoJSON 格式的数据:

echo json_encode(array('id'=>$id,'name'=>$name));

UPDATEin your case use the following code: (not tested)

在您的情况下更新使用以下代码:(未测试)

php code:

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 code:

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('');
   }
}

USING jQuery

使用 jQuery

javascript code:

javascript代码:

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('');
   }
}

回答by EveryWell

The best way would probably be with an ajax call, anyway if you are declaring the script in the same page with the php, you can json encode the array with the options so that you will be able to access it into the javascript, like this:

最好的方法可能是使用ajax调用,无论如何,如果您在与php相同的页面中声明脚本,您可以使用选项对数组进行json编码,以便您能够将其访问到javascript中,就像这样:

var optionIds = <php echo json_encode($id); ?>
var optionNames = <php echo json_encode($name); ?>