栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

在javascript中,您必须

ajax call
对您的php文件进行操作:

var xmlhttp;if (window.XMLHttpRequest)  {// pre for IE7+, Firefox, Chrome, Opera, Safari  xmlhttp=new XMLHttpRequest();  }else  {// pre 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文件中,您必须以

echo
JSON格式存储数据:

echo json_enpre(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_enpre($options);?>

Javascript代码:

var xmlhttp;if (window.XMLHttpRequest){// pre for IE7+, Firefox, Chrome, Opera, Safari  xmlhttp=new XMLHttpRequest();}else{// pre 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 >';      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

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 >';for( var index in response ){    select = select + "<option value='"+ index +"'>"+response[index]+"</option>";}select += "</select>";$("#send2").html(select);       });   }    else {      $('#send2').html('');   }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/614401.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号