当前位置: 移动技术网 > IT编程>开发语言>PHP > 原生JS实现Ajax通过GET方式与PHP进行交互操作示例

原生JS实现Ajax通过GET方式与PHP进行交互操作示例

2018年06月16日  | 移动技术网IT编程  | 我要评论

本文实例讲述了原生js实现ajax通过get方式与php进行交互操作。分享给大家供大家参考,具体如下:

一、代码

conn.php

<?php
   $conn=mysql_connect("localhost","root","root") or die("数据库连接失败".mysql_error());
   mysql_select_db("db_database27",$conn) or die("数据库连接失败".mysql_error());
   mysql_query("set names gb2312");
?>

index.php

<!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>通过xmlhttprequest对象读取html文件,并且输出读取结果</title>
<style type="text/css">
<!--
body {
  margin-left: 0px;
  margin-top: 00px;
  margin-right: 0px;
  margin-bottom: 0px;
}
-->
</style></head>
<script>
var xmlhttp;        //定义xmlhttprequest对象
function createxmlhttprequestobject(){
  //如果在internet explorer下运行
  if(window.activexobject){
    try{
      xmlhttp=new activexobject("microsoft.xmlhttp");
    }catch(e){
      xmlhttp=false;
    }
  }else{
  //如果在mozilla或其他的浏览器下运行
    try{
      xmlhttp=new xmlhttprequest();
    }catch(e){
      xmlhttp=false;
    }
  }
   //返回创建的对象或显示错误信息
  if(!xmlhttp)
    alert("返回创建的对象或显示错误信息");
    else
    return xmlhttp;
}
function showsimple(){
  createxmlhttprequestobject();
  var cont = document.getelementbyid("searchtxt").value;
  if(cont==""){
    alert('查询关键字不能为空!');
    return false;
  }
    xmlhttp.onreadystatechange=stathandler; //判断url调用的状态值并处理
    xmlhttp.open("get",'searchrst.php?cont='+cont,false);
    xmlhttp.send(null);
}
function stathandler(){
  if(xmlhttp.readystate==4 && xmlhttp.status==200){
    document.getelementbyid("webpage").innerhtml=xmlhttp.responsetext;
  }
}
</script>
<body>
<table width="800" height="632" border="0" align="center" cellpadding="0" cellspacing="0" background="images/bj.jpg">
 <tr>
  <td width="260" height="245"> </td>
  <td width="500" align="center" valign="bottom"><strong>查询员工信息,根据员工技能信息</strong></td>
  <td width="40"> </td>
 </tr><form id="searchform" name="searchform" method="get" action="#">
 <tr>
  <td height="40"> </td>
  <td align="center">请输入关键字: <input name="searchtxt" type="text" id="searchtxt" size="30" />
    <input id="s_search" name="s_search" type="button" value="查询" onclick="return showsimple()" /></td>
  <td> </td>
 </tr> </form>
 <tr>
  <td height="268"> </td>
  <td align="center" valign="top"><div id="webpage"></div></td>
  <td> </td>
 </tr>
 <tr>
  <td> </td>
  <td> </td>
  <td> </td>
 </tr>
</table>
</body>
</html>

searchrst.php

<?php
header('content-type: text/html;charset=gb2312');        //指定发送数据的编码格式
  include_once 'conn/conn.php';                //连接数据库
    $cont = $_get['cont'];                 //获取ajax传递的查询关键字
    if(!empty($_get['cont'])){               //判断如果关键字不为空
      $sql = "select * from tb_administrator where explains like '%".$cont."%'"; //定义sql语句
      $result=mysql_query($sql,$conn);          //执行模糊查询
      if(mysql_num_rows($result)>0){        //获取查询结果
        echo "<table width='500' border='1' cellpadding='1' cellspacing='1' bordercolor='#ffffcc' bgcolor='#666666'>";
        echo "<tr><td height='30' align='center' bgcolor='#ffffff'>id</td><td align='center' bgcolor='#ffffff'>名称</td><td align='center' bgcolor='#ffffff'>编号</td><td align='center' bgcolor='#ffffff'>描述</td></tr>";
 while($myrow=mysql_fetch_array($result)){           //循环输出查询结果
 echo "<tr><td height='22' bgcolor='#ffffff'>".$myrow[id]."</td>";
 echo "<td bgcolor='#ffffff'>".$myrow[user]."</td>";
  echo "<td bgcolor='#ffffff'>".$myrow[number]."</td>";
echo "<td bgcolor='#ffffff'>".$myrow[explains]."</td>";
echo "</tr>";
}
echo "</table>";
      }else{
      echo "没有符合条件的数据";
      }
    }
?>

二、运行结果

更多关于php相关内容感兴趣的读者可查看本站专题:《php+ajax技巧与应用小结》、《php网络编程技巧总结》、《php基本语法入门教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家php程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网