当前位置:Gxlcms > 数据库问题 > PHP 和 AJAX MySQL

PHP 和 AJAX MySQL

时间:2021-07-01 10:21:17 帮助过:19人阅读

test.html

 1 <! DOCTYPE HTML>
 2 <html>
 3 <head>
 4 <script src="test.js"></script>
 5 
 6 </head>
 7 
 8 
 9 <body>
10 <select name="users" onchange="showUser(this.value)">
11 <option value="1" >peter</option>
12 <option value="2">lois</option>
13 <option value="3">joseph</option>
14 
15 </select>
16 <p>
17 <div id="txtHint">info:<br/>
18 </div>
19 </p>
20 </body>
21 </html>

 

test.js

 

 1 var xmlHttp;
 2 function showUser(str) {
 3     xmlHttp=GetXmlHttpObject();
 4     if(xmlHttp == null)
 5     {        alert("not support");
 6     return;
 7     }
 8     var url = "test.php";
 9     url = url +"?q=" +str;
10     url = url +"&sid=" +Math.random();
11     xmlHttp.onreadystatechange = stateChanged;
12     xmlHttp.open("GET",url,true);
13     xmlHttp.send(null);
14     }
15 
16 
17 function stateChanged() {
18     if(xmlHttp.readyState == 4||xmlHttp.readyState == "complete")
19         document.getElementById("txtHint").innerHTML = xmlHttp.responseText;
20 }
21 
22 function GetXmlHttpObject() {
23     var xmlHttp;
24     try {
25         xmlHttp = new XMLHttpRequest();
26     }
27     catch(e) {
28         try {
29             xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
30         }
31         catch(e) {
32             xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
33         }
34     }
35     return xmlHttp;
36 }

 

test.php

 1 <html>
 2 <head>
 3 
 4 </head>
 5 
 6 
 7 <body>
 8 
 9 <?php 
10 $q = $_GET[‘q‘];
11 
12 $con = mysql_connect("localhost","root","123456");
13 if(!$con)
14     alert("not connect".mysql_error);
15 mysql_select_db("test",$con);
16 $sql = "select * from ranking where Id=‘$q‘";
17 $result = mysql_query($sql);
18 while($row = mysql_fetch_array($result))
19 {
20     echo "id:".$q."====".$row[‘Id‘]."<br/>FName:".$row[‘FName‘]."<br/>Age:".$row[‘Age‘]."<br/>";
21 }
22 
23 
24 mysql_close();
25     
26 function Alert($Str,$Typ="back",$TopWindow="",$Tim=100){
27 echo "<script>".chr(10);
28   if(!empty($Str)){
29     echo "alert(\"Warning:\\n\\n{$Str}\\n\\n\");".chr(10);
30   }
31   echo "function _r_r_(){";
32   $WinName=(!empty($TopWindow))?"top":"self";
33   switch (StrToLower($Typ)){
34   case "#":
35     break;
36   case "back":
37     echo $WinName.".history.go(-1);".chr(10);
38     break;
39   case "reload":
40     echo $WinName.".window.location.reload();".chr(10);
41     break;
42   case "close":
43     echo "window.opener=null;window.close();".chr(10);
44     break;
45   case "function":
46     echo "var _T=new function(‘return {$TopWindow}‘)();_T();".chr(10);
47     break;
48     //Die();
49   Default:
50     if($Typ!=""){
51       //echo "window.{$WinName}.location.href=‘{$Typ}‘;";
52       echo "window.{$WinName}.location=(‘{$Typ}‘);";
53     }
54   }
55   echo "}".chr(10);
56   //為防止Firefox不執行setTimeout
57   echo "if(setTimeout(\"_r_r_()\",".$Tim.")==2){_r_r_();}";
58   if($Tim==100){
59     echo "_r_r_();".chr(10);
60   }else{
61     echo "setTimeout(\"_r_r_()\",".$Tim.");".chr(10);
62   }
63   echo "</script>".chr(10);
64   Exit();
65 }
66 ?>
67 </body>
68 </html>

MySQL 预配置

 

技术分享

 

PHP 和 AJAX MySQL

标签:new   oca   下拉   代码执行   object   job   url   logs   array   

人气教程排行