 
- PHP教程
- PHP-主页
- PHP - 简介
- PHP-环境设置
- PHP - 语法概述
- PHP - 变量类型
- PHP - 常量
- PHP - 运算符类型
- PHP - 决策
- PHP - 循环类型
- PHP-数组
- PHP-字符串
- PHP - 网络概念
- PHP - 获取和发布
- PHP - 文件包含
- PHP - 文件和 I/O
- PHP - 函数
- PHP-Cookie
- PHP-会话
- PHP - 发送电子邮件
- PHP-文件上传
- PHP - 编码标准
- 高级PHP
- PHP - 预定义变量
- PHP-正则表达式
- PHP - 错误处理
- PHP - 错误调试
- PHP - 日期和时间
- PHP 和 MySQL
- PHP 和 AJAX
- PHP 和 XML
- PHP——面向对象
- PHP - 面向 C 开发人员
- PHP - 适合 PERL 开发人员
- PHP 表单示例
- PHP-表单介绍
- PHP - 验证示例
- PHP - 完整表格
- PHP框架作品
- PHP-框架工程
- PHP - 核心 PHP 与 Frame Works
- PHP 设计模式
- PHP - 设计模式
- PHP 函数参考
- PHP - 内置函数
- PHP 有用资源
- PHP - 问题与解答
- PHP - 有用的资源
- PHP - 讨论
PHP 和 AJAX
什么是 AJAX?
- AJAX代表同步JavaScript和XML。AJAX 是一种借助 XML、HTML、CSS 和 Java Script 创建更好、更快、交互性更强的 Web 应用程序的新技术。 
- 传统的 Web 应用程序使用同步请求向服务器传输信息或从服务器传输信息。这意味着您填写表格,点击提交,然后定向到包含来自服务器的新信息的新页面。 
- 使用 AJAX,当按下提交时,JavaScript 将向服务器发出请求,解释结果并更新当前屏幕。从最纯粹的意义上来说,用户永远不会知道任何内容都被传输到了服务器。 
关于AJAX的完整学习,请参考AJAX教程。
PHP 和 AJAX 示例
为了清楚地说明使用 Ajax 和 PHP 从数据库访问信息是多么容易,我们将动态构建 MySQL 查询并将结果显示在“ajax.html”上。但在我们继续之前,让我们先做一些基础工作。使用以下命令创建表。
注意- 我们假设您有足够的权限来执行以下 MySQL 操作。
CREATE TABLE `ajax_example` ( `name` varchar(50) NOT NULL, `age` int(11) NOT NULL, `sex` varchar(1) NOT NULL, `wpm` int(11) NOT NULL, PRIMARY KEY (`name`) )
现在使用以下 SQL 语句将以下数据转储到该表中。
INSERT INTO `ajax_example` VALUES ('Jerry', 120, 'm', 20);
INSERT INTO `ajax_example` VALUES ('Regis', 75, 'm', 44);
INSERT INTO `ajax_example` VALUES ('Frank', 45, 'm', 87);
INSERT INTO `ajax_example` VALUES ('Jill', 22, 'f', 72);
INSERT INTO `ajax_example` VALUES ('Tracy', 27, 'f', 0);
INSERT INTO `ajax_example` VALUES ('Julie', 35, 'f', 90);
 
客户端 HTML 文件
现在让我们的客户端 HTML 文件是 ajax.html,它将包含以下代码
<html>
   <body>
      
      <script language = "javascript" type = "text/javascript">
         <!--
            //Browser Support Code
            function ajaxFunction(){
               var ajaxRequest;  // The variable that makes Ajax possible!
               
               try {
                  // Opera 8.0+, Firefox, Safari
                  ajaxRequest = new XMLHttpRequest();
               }catch (e) {
                  // Internet Explorer Browsers
                  try {
                     ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
                  }catch (e) {
                     try{
                        ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                     }catch (e){
                        // Something went wrong
                        alert("Your browser broke!");
                        return false;
                     }
                  }
               }
               
               // Create a function that will receive data 
               // sent from the server and will update
               // div section in the same page.
					
               ajaxRequest.onreadystatechange = function(){
                  if(ajaxRequest.readyState == 4){
                     var ajaxDisplay = document.getElementById('ajaxDiv');
                     ajaxDisplay.innerHTML = ajaxRequest.responseText;
                  }
               }
               
               // Now get the value from user and pass it to
               // server script.
					
               var age = document.getElementById('age').value;
               var wpm = document.getElementById('wpm').value;
               var sex = document.getElementById('sex').value;
               var queryString = "?age=" + age ;
            
               queryString +=  "&wpm=" + wpm + "&sex=" + sex;
               ajaxRequest.open("GET", "ajax-example.php" + queryString, true);
               ajaxRequest.send(null); 
            }
         //-->
      </script>
		
      <form name = 'myForm'>
         Max Age: <input type = 'text' id = 'age' /> <br />
         Max WPM: <input type = 'text' id = 'wpm' />
         <br />
         
         Sex: <select id = 'sex'>
            <option value = "m">m</option>
            <option value = "f">f</option>
         </select>
			
         <input type = 'button' onclick = 'ajaxFunction()' value = 'Query MySQL'/>
			
      </form>
      
      <div id = 'ajaxDiv'>Your result will display here</div>
   </body>
</html>
注意- 在查询中传递变量的方式是根据 HTTP 标准并且具有 formA。
URL?variable1=value1;&variable2=value2;
现在上面的代码会给你一个如下所示的屏幕
注意- 这是虚拟屏幕,无法工作。
您的结果将显示在这里
服务器端 PHP 文件
现在您的客户端脚本已准备就绪。现在我们必须编写服务器端脚本,该脚本将从数据库中获取年龄、wpm 和性别,并将其发送回客户端。将以下代码放入“ajax-example.php”文件中。
<?php
   
   $dbhost = "localhost";
   $dbuser = "dbusername";
   $dbpass = "dbpassword";
   $dbname = "dbname";
   
   //Connect to MySQL Server
   mysql_connect($dbhost, $dbuser, $dbpass);
   
   //Select Database
   mysql_select_db($dbname) or die(mysql_error());
   
   // Retrieve data from Query String
   $age = $_GET['age'];
   $sex = $_GET['sex'];
   $wpm = $_GET['wpm'];
   
   // Escape User Input to help prevent SQL Injection
   $age = mysql_real_escape_string($age);
   $sex = mysql_real_escape_string($sex);
   $wpm = mysql_real_escape_string($wpm);
   
   //build query
   $query = "SELECT * FROM ajax_example WHERE sex = '$sex'";
   
   if(is_numeric($age))
   $query .= " AND age <= $age";
   
   if(is_numeric($wpm))
   $query .= " AND wpm <= $wpm";
   
   //Execute query
   $qry_result = mysql_query($query) or die(mysql_error());
   
   //Build Result String
   $display_string = "<table>";
   $display_string .= "<tr>";
   $display_string .= "<th>Name</th>";
   $display_string .= "<th>Age</th>";
   $display_string .= "<th>Sex</th>";
   $display_string .= "<th>WPM</th>";
   $display_string .= "</tr>";
   
   // Insert a new row in the table for each person returned
   while($row = mysql_fetch_array($qry_result)) {
      $display_string .= "<tr>";
      $display_string .= "<td>$row[name]</td>";
      $display_string .= "<td>$row[age]</td>";
      $display_string .= "<td>$row[sex]</td>";
      $display_string .= "<td>$row[wpm]</td>";
      $display_string .= "</tr>";
   }
   echo "Query: " . $query . "<br />";
   
   $display_string .= "</table>";
   echo $display_string;
?>
 
现在尝试在“Max Age”或任何其他框中输入有效值,然后单击“查询 MySQL”按钮。
您的结果将显示在这里
如果您成功完成了本课程,那么您就知道如何协同使用 MySQL、PHP、HTML 和 Javascript 来编写 Ajax 应用程序。
