html+js+php一次原始的Ajax請求樣本

來源:互聯網
上載者:User

 雖然jquery的ajax要比原始的寫法容易得多,我們還是應該瞭解原始的寫法,下面有個不錯的樣本,大家可以參考下

今天給大家呈現一個原始的Ajax請求過程,雖然jquery的ajax要比原始的寫法容易得多,我們還是應該瞭解原始的寫法,下面我分為html、js、php三個小檔案來展示,資料庫自己寫。 

 

首先是html: 

 代碼如下:

<html> 

<head> 

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 

<title>一次簡單的Ajax請求</title> 

<script language="javascript" src="js/ajaxTest.js"></script> 

</head> 

<body> 

使用者名稱:<input id="userName" type="text"></input> 

密碼:<input id="passWord" type="password"></input> 

<span id="showInputError" style="color:red;font-weight: bold;"></span><br> 

<input type="button" value="登入" onclick="showSelect()"> 

</body> 

</html> 

 

然後是js: 

 代碼如下:

/** 

* 普通Ajax的完整訪問過程 

*/ 

var xmlHttp 

 

function showSelect()//登入按鈕點擊後執行這個方法 

{ 

var userName=document.getElementById("userName").value; 

var passWord=document.getElementById("passWord").value; 

if (userName.length==0)//驗證輸入使用者名稱是否為空白 

{ 

document.getElementById("showInputError").innerHTML="使用者名稱不可為空";//提示使用者名稱不可為空 

return 

} 

xmlHttp=GetXmlHttpObject() 

if (xmlHttp==null) 

{ 

alert ("Browser does not support HTTP Request") 

return 

} 

var url="ajaxTest.php"//設定要提交action到背景那個處理請求的檔案名稱 

url=url+"?userName="+userName+"&passWord="+passWord//為這個路徑加上參數使用者名稱和密碼 

url=url+"&sid="+Math.random()//為這個路徑加上一個隨機數 

xmlHttp.onreadystatechange=stateChanged//每當 readyState 改變時,就會觸發 onreadystatechange 事件,readyState 屬性存有 XMLHttpRequest 的狀態資訊 

xmlHttp.open("GET",url,true)//定義請求的參數 

xmlHttp.send(null)//發送請求 

} 

 

function stateChanged() 

{ 

if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")// 

//0: 請求未初始化 

//1: 伺服器串連已建立 

//2: 請求已接收 

//3: 請求處理中 

//4: 請求已完成,且響應已就緒 

{ var a= xmlHttp.responseText;//把相應資料賦值給a 

if(a=="yes"){ 

self.location='Main.php';//跳轉到Main.php 

}else{ 

document.getElementById("showInputError").innerHTML="使用者名稱或密碼錯誤";//提示使用者名稱或密碼錯誤 

} 

} 

} 

function GetXmlHttpObject() 

{ 

var xmlHttp=null; 

try 

{ 

// Firefox, Opera 8.0+, Safari 

xmlHttp=new XMLHttpRequest(); 

} 

catch (e) 

{ 

// Internet Explorer 

try 

{ 

xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); 

} 

catch (e) 

{ 

xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); 

} 

} 

return xmlHttp; 

} 

 

最後是php: 

 代碼如下:

<?php 

$userName=$_GET["userName"]; 

$pwd=$_GET["passWord"]; 

$con = mysql_connect("localhost","root","123456"); 

mysql_select_db("my_test", $con); 

mysql_query("set names utf8"); 

$sql="SELECT * FROM Userinfo WHERE userName='".$userName."' AND pwd='".$pwd."'"; 

$result=mysql_query($sql); 

$select=mysql_num_rows($result); 

$a="no"; 

if ($select>0){$a="yes";} 

echo $a; 

mysql_close($con); 

?> 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.