php/ajax/mysql---有很关键的ajax乱码解决哦

本文提供了一个使用Ajax与MySQL数据库进行交互的示例代码,包括前端HTML页面和后端PHP脚本。前端通过Ajax发送请求给服务器,后端根据传来的参数查询数据库并返回结果。
 
算翻译吧,我改了不少东西,还解决其中的乱码问题,嘿嘿,算原创吧,忽忽。。。
 
ajax.html:
 
< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > 
< html  xmlns ="http://www.w3.org/1999/xhtml" > 
< head > 
< meta  http-equiv ="Content-Type"  content ="text/html; charset=gb2312"  /> 
< title >Ajax教程 </title> 
< script  language ="javascript"  type ="text/javascript" > 
//浏览器支持代码 
function ajaxFunction(){ 
  var ajaxRequest;    //xmlHttpRequest对象 
    
  try{ 
    // Opera 8.0+, Firefox, Safari 
    ajaxRequest = new XMLHttpRequest(); 
  } catch (e){ 
    // IE系列 
    try{ 
      ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
    } catch (e) { 
      try{ 
        ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
      } catch (e){ 
        // 古老的浏览器 
        alert("你的浏览器不支持Ajax!"); 
        return false; 
      } 
    } 
  } 
    
  // 创建一个函数负责介绍来自服务器的数据 
  ajaxRequest.onreadystatechange = function(){ 
    if(ajaxRequest.readyState == 4){ 
      var ajaxDisplay = document.getElementById('ajaxDiv'); 
      ajaxDisplay.innerHTML = ajaxRequest.responseText; 
    } 
  } 

  var age = document.getElementById('age').value; 
  var wpm = document.getElementById('wpm').value; 
  var sex = document.getElementById('sex').value; 
  var queryString = "?age=" + age + "&wpm=" + wpm + "&sex=" + sex; 
  ajaxRequest.open("GET", "ajaxmysql.php" + queryString, true);    
  ajaxRequest.send(null);    

</script> 
</head> 
< body > 
< form  name ='myForm' > 
最大年龄:  < input  type ='text'  id ='age'  />  < br  /> 
最大打字速度WPM(words per minute):  < input  type ='text'  id ='wpm'  /> 
< br  /> 
性别:  < select  id ='sex' > 
< option  value ='m' ></option> 
< option  value ='f' ></option> 
</select> 
< input  type ='button'  onclick ='ajaxFunction()'  value ='查询mysql'  /> 
</form> 
< div  id ='ajaxDiv' >这里显示结果. </div> 
</body> 
</html> 

 
前台的字符集编码是国标gb2312保存的按gb2312编码保存(用的emeditor编辑器).
 
后台页面ajaxmysql.php:
 
InBlock.gif<?php 
InBlock.gifheader( "Content-Type:text/html;charset=utf-8"); //页面的字符集编码 
InBlock.gif$dbhost =  "localhost"
InBlock.gif$dbuser =  "root"
InBlock.gif$dbpass =  "123456"
InBlock.gif$dbname =  "ajax"
InBlock.gif //连接mysql 
InBlock.gifmysql_connect($dbhost, $dbuser, $dbpass); 
InBlock.gif //选择数据库 
InBlock.gifmysql_select_db($dbname) or die(mysql_error()); 
InBlock.gif 
InBlock.gifmysql_query( "set names utf8"); //防止查询乱码 
InBlock.gif 
InBlock.gif //检索 查询串 
InBlock.gif$age = $_GET['age']; 
InBlock.gif$sex = $_GET['sex']; 
InBlock.gif$wpm = $_GET['wpm']; 
InBlock.gif 
InBlock.gif // 防止注入 
InBlock.gif$age = mysql_real_escape_string($age); 
InBlock.gif$sex = mysql_real_escape_string($sex); 
InBlock.gif$wpm = mysql_real_escape_string($wpm); 
InBlock.gif 
InBlock.gif //构造查询 
InBlock.gif$query =  "SELECT * FROM ex1 WHERE sex = '$sex'"
InBlock.gif if(is_numeric($age)) 
InBlock.gif$query .=  " AND age <= $age"
InBlock.gif if(is_numeric($wpm)) 
InBlock.gif$query .=  " AND wpm <= $wpm"
InBlock.gif 
InBlock.gif //执行查询 
InBlock.gif$qry_result = mysql_query($query) or die(mysql_error()); 
InBlock.gif 
InBlock.gif //结果串 
InBlock.gif$display_string =  "<table>"
InBlock.gif$display_string .=  "<tr>"
InBlock.gif$display_string .=  "<th>姓名</th>"
InBlock.gif$display_string .=  "<th>年龄</th>"
InBlock.gif$display_string .=  "<th>性别</th>"
InBlock.gif$display_string .=  "<th>打字速度</th>"
InBlock.gif$display_string .=  "</tr>"
InBlock.gif 
InBlock.gif // 为每一个返回的人插入一行. 
InBlock.gif while($row = mysql_fetch_array($qry_result)){ 
InBlock.gif  $display_string .=  "<tr>"
InBlock.gif  $display_string .=  "<td>{$row['name']}</td>"
InBlock.gif  $display_string .=  "<td>{$row['age']}</td>"
InBlock.gif  $display_string .=  "<td>{$row['sex']}</td>"
InBlock.gif  $display_string .=  "<td>{$row['wpm']}</td>"
InBlock.gif  $display_string .=  "</tr>"
InBlock.gif
InBlock.gifecho  "查询: " . $query .  "<br />"
InBlock.gif$display_string .=  "</table>"
InBlock.gifecho $display_string; 
InBlock.gif?> 
 
后台页面编码utf-8.
 
主要考虑ajax的utf8做的改动。
 
 
相关截图:
 
 
 
 
 
 
本文转自 xcf007 51CTO博客,原文链接:http://blog.51cto.com/xcf007/110868,如需转载请自行联系原作者





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值