AJAX 是什么?
- AJAX 代表异步 JavaScript 和 XML。AJAX 是一种新技术,它是在 XML、HTML、CSS 和 Java 帮助下可以创建更好、更快、交互式 web 应用程序的脚本语言。
- 传统 web 应用程序使用同步请求从服务器传送信息。这意味着你填写表格,点击提交,获得定向到一个新的从服务器获取信息页面。
- 使用 AJAX 提交按钮被按下时,JavaScript 将发送一个请求到服务器,解释结果和更新当前屏幕。在纯粹意义上,用户甚至不会知道什么时候把请求传送到服务器上。
PHP and AJAX 例子
明确说明使用 AJAX 和 PHP 是非常容易的从数据库中获取信息,我们要在 “ajax.html” 建立动态 MysqL 查询和显示结果。但在我们继续进行工作之前,让 AJAX 做下工作。可以使用以下命令创建一个表。
注释:我们确保您有足够的权限来执行 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,44); INSERT INTO `ajax_example` VALUES ('Frank',45,87); INSERT INTO `ajax_example` VALUES ('Jill',22,'f',72); INSERT INTO `ajax_example` VALUES ('Tracy',27,0); INSERT INTO `ajax_example` VALUES ('Julie',35,90);
客户端 HTML 文件
现在可以让我们的客户端 HTML 文件创建 ajax。如下是 html 和它的代码:
s<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 标准和形式。
URL?variable1=value1;&variable2=value2;
服务器端 PHP 文件
现在你的客户端脚本已经准备好了。现在我们必须编写服务器端脚本将获取的 age, wpm 和 sex 从数据库取出并将其发送回客户端。把以下代码放入到 “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; ?>