[Ajax代码]用PHP实现简单的无刷新搜索

jackxiang 2006-12-14 11:17 | |

点击在新窗口中浏览此图片

testme.html
code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>
ajax
</title>
<script language="javascript">
function createRequest(){
       if(typeof XMLHttpRequest!="undefined")        {
               return new XMLHttpRequest();
       }else if(typeof ActiveXObject!="undefined"){
               var xmlHttp_ver  = false;
               var xmlHttp_vers = ["MSXML2.XmlHttp.5.0","MSXML2.XmlHttp.4.0","MSXML2.XmlHttp.3.0","MSXML2.XmlHttp","Microsoft.XmlHttp"];
               if(!xmlHttp_ver){
                       for(var i=0;i<xmlHttp_vers.length;i++){
                               try{
                                       new ActiveXObject(xmlHttp_vers[i]);
                                       xmlHttp_ver = xmlHttp_vers[i];
                                       break;
                               }catch(oError){;}
                       }
               }
               if(xmlHttp_ver){
                       return new ActiveXObject(xmlHttp_ver);
               }else{
                       throw new Error("Could not create XML HTTP Request.");
               }
       }else{
               throw new Error("Your browser doesn't support an XML HTTP Request.");
       }
}

var xmlHttp;

function sendPostRequest()
{    
                    xmlHttp = createRequest();
       var shownum=document.getElementById("shownum").value;// html页面中一个id为shownum的input表单
       var url = "http://localhost/phpwork/phpajaxtest.php";//要发送到的URL
       var queryString = "shownum" + "=" + shownum;
       //向服务端发送请求
       xmlHttp.open("post", url, true);//这里的第三个参数为true为异步方式处理
       xmlHttp.onreadystatechange = showData;//异步方式处理,当状态改变时会调用onreadystatechange属性指定的回调函数showData
       xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");//这一句是用post方法发送的时候必须写的
       xmlHttp.send(queryString);//发送你构建成的数据,如果为“get”方法时,这里可以写成xmlHttp.send(NULL);
}
function showData()
{
       var msg=document.getElementById("status");
                    //第4步
       if(xmlHttp.readyState==4)
       {
               if(xmlHttp.status==200)
               {
                //只有当readyState为4并且status为200时,才表示符合要求
                                                              //下面这一句话,就相当于上面说的第5步,处理返回的结果
                       msg.innerHTML =  xmlHttp.responseText;
               }
       }
       else
       {
               switch(xmlHttp.readyState)
               {
               case 0:
                       msg.innerHTML="未初始化...";
                       break;
               case 1:
                       msg.innerHTML="加载中...";
                       break;
               case 2:
                       msg.innerHTML="连接完成...";
                       break;
               case 3:
                       msg.innerHTML="交换数据...";
                       break;
               default:
                       break;
               }
       }
}

</script>
</head>
<body>
<div>要发送的数据:<input name="shownum" type="text" id="shownum" value="root">
<input type="button" name="search" value="发送" onClick="sendPostRequest()">
(输入root,xdy108)</div>
<div id="status"></div>
</body>
</html>
phpajaxtest.php
code:
<?php require_once('Connections/conn.php'); ?>
<?php
$colname_Recordset1 = "-1";
if (isset($_POST['shownum'])) {
 $colname_Recordset1 = (get_magic_quotes_gpc()) ? $_POST['shownum'] : addslashes($_POST['shownum']);
}
mysql_select_db($database_conn, $conn);
$query_Recordset1 = sprintf("SELECT `Host`, `User`, Password, Select_priv FROM `user` WHERE `User` = '%s'", $colname_Recordset1);
$Recordset1 = mysql_query($query_Recordset1, $conn) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<!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>无标题文档</title>
</head>

<body>
<table border="1">
 <tr>
   <td>Host</td>
   <td>User</td>
   <td>Password</td>
   <td>Select_priv</td>
 </tr>
 <?php do { ?>
   <tr>
     <td><?php echo $row_Recordset1['Host']; ?></td>
     <td><?php echo $row_Recordset1['User']; ?></td>
     <td><?php echo $row_Recordset1['Password']; ?></td>
     <td><?php echo $row_Recordset1['Select_priv']; ?></td>
   </tr>
   <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

conn.php
code:
<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_conn = "localhost";
$database_conn = "mysql";
$username_conn = "root";
$password_conn = "12345";
$conn = mysql_pconnect($hostname_conn, $username_conn, $password_conn) or trigger_error(mysql_error(),E_USER_ERROR);
?>

user.sql
code:
# phpMyAdmin SQL Dump
# version 2.5.7
# http://www.phpmyadmin.net
#
# 主机: localhost
# 生成日期: 2006 年 12 月 13 日 05:58
# 服务器版本: 5.0.19
# PHP 版本: 5.1.2
#
# 数据库 : `mysql`
#

# --------------------------------------------------------

#
# 表的结构 `user`
#

CREATE TABLE `user` (
 `Host` char(60) collate utf8_bin NOT NULL default '',
 `User` char(16) collate utf8_bin NOT NULL default '',
 `Password` char(41) character set latin1 collate latin1_bin NOT NULL default '',
 `Select_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
 `Insert_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
 `Update_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
 `Delete_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
 `Create_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
 `Drop_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
 `Reload_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
 `Shutdown_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
 `Process_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
 `File_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
 `Grant_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
 `References_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
 `Index_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
 `Alter_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
 `Show_db_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
 `Super_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
 `Create_tmp_table_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
 `Lock_tables_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
 `Execute_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
 `Repl_slave_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
 `Repl_client_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
 `Create_view_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
 `Show_view_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
 `Create_routine_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
 `Alter_routine_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
 `Create_user_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
 `ssl_type` enum('','ANY','X509','SPECIFIED') character set utf8 NOT NULL default '',
 `ssl_cipher` blob NOT NULL,
 `x509_issuer` blob NOT NULL,
 `x509_subject` blob NOT NULL,
 `max_questions` int(11) unsigned NOT NULL default '0',
 `max_updates` int(11) unsigned NOT NULL default '0',
 `max_connections` int(11) unsigned NOT NULL default '0',
 `max_user_connections` int(11) unsigned NOT NULL default '0',
 PRIMARY KEY  (`Host`,`User`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Users and global privileges';

#
# 导出表中的数据 `user`
#

INSERT INTO `user` VALUES ('localhost', 'root', '*00A51F3F48415C7D4E8908980D443C29C69B60C9', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', '', '', '', '', 0, 0, 0, 0);
INSERT INTO `user` VALUES ('localhost', 'xdy108', '001002', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', '', '', '', '', 0, 0, 0, 0);


发现输入xdy108,root的时候能无刷新返回,否则如xdy,ro等就会无返回结果,其原因是用了:WHERE `User` = '%s'", $colname_Recordset1);没有用like.
如下图:

点击在新窗口中浏览此图片

作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/403/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!


最后编辑: jackxiang 编辑于2006-12-14 11:23
评论列表
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]