PHP的ajax

來源:互聯網
上載者:User

標籤:style   c   code   java   http   ext   

代碼如下

先建立一個表單

 

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
<script type="text/javascript" src="ajax.js"></script>
</head>

<body>

<form action="" >
使用者名稱: <input type="text" name="user" id="" onblur="sendParam(this.value)"/>
<span style="color: red;font-size: 14px" id="tishi"></span>
<br/>
密 碼: <input type="password" name="mima"/> <br/>

<input type="submit" value="提交" name="submit"/>
</form>
</body>

</html>

 

然後

資料庫的基礎語言   

<?php
if(isset($_GET[‘user‘])){
$user = $_GET[‘user‘];

$conn = mysql_connect("localhost","root","") or die("資料庫連接失敗".mysql_error());
mysql_select_db("test");
mysql_query("set names ‘utf8‘");

$sql = "select count(*) from bbs where userName=‘$user‘";

$result = mysql_query($sql);
$row = mysql_fetch_row($result);

if($row[0]){
echo "該使用者名稱已經被註冊";
}else{
echo "該使用者名稱還沒有被使用過,可以註冊";
}
}
?>

 

最後AJAX

 


var xmlHttp;
function getXmlHttp(){
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("MICROSOFT.XMLHTTP");
}else if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
}

function sendParam(url){
getXmlHttp();

xmlHttp.open("GET","check.php?user="+url,true);
// setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.onreadystatechange = getTxt;
xmlHttp.send(null);
}
function getTxt(){
if(xmlHttp.readyState == 4){
if(xmlHttp.status == 200){
var sp = document.getElementById("tishi");
sp.innerHTML = xmlHttp.responseText;
}
}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.