ajax實現的點擊數目加1代碼執行個體,ajax執行個體

來源:互聯網
上載者:User

ajax實現的點擊數目加1代碼執行個體,ajax執行個體

ajax實現的點擊數目加1代碼執行個體:
在點擊按鈕實現數字增加效果代碼執行個體一章節中,介紹如何點擊按鈕實現數字加1的效果,但是好像並沒有什麼實際用處,下面就分享一段相對完整的能夠在實際應用中派上用場的代碼,此代碼是ajax結合php代碼實現的。
一.ajax代碼如下:

<!DOCTYPE html><html><head><meta charset=" utf-8"><meta name="author" content="http://www.softwhy.com/" /><title>螞蟻部落</title><script type="text/javascript">var xmlhttp=false;function add(){  try{    xmlhttp= new XMLHttpRequest;  }  catch(e){    xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");  }   xmlhttp.open('GET','count.php?a=a',false);  xmlhttp.onreadystatechange=func;  xmlhttp.send(null);} function func(){  if(xmlhttp.readyState==4){    var msg=xmlhttp.responseText;    var tt=document.getElementById("num");    tt.innerHTML=msg;  }}</script></head><body>當前頁面資料庫中訪問次數:<div id='num'></div><input type="button" value="增加次數" ></body></html>

 二.php代碼:

<?php  mysql_connect('localhost','root','');  mysql_selectdb('click');  $rs=mysql_query("UPDATE click SET num = num +1 WHERE name = '".$_GET['a']."'");  if(mysql_affected_rows()==1){    $rs=mysql_query("select * from click where name='".$_GET['a']."'");    $row=mysql_fetch_array($rs);    echo $row['num'];  }?>

ajax的執行個體代碼?

//核心物件變數
var xmlHttp;

//區分瀏覽器建立XMLHttpRequest核心對象
function create(){
if(window.XMLHttpRequset){
xmlHttp = new XMLHttpRequest();
}else if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}

//ajax核心執行方法(此處為提交到servlet處理後,返回純文字)
function run(){
create();
var value = document.getElementById("id").value;
var URL = "/ajax/servlet/ajax?name="+value;
xmlHttp.open("GET",URL,true);
xmlHttp.onreadystatechange=callback;
xmlHttp.send(null);
}

//回呼函數
function callback(){
if(xmlHttp.readyState == 4){
if(xmlHttp.status == 200){
var v = xmlHttp.responseText;
alert(v);
}
}
}
 
誰有比較簡單的AJAX代碼 執行個體

js 部分
//產生Ajax的調用對象
function creatAjax()
{
var HttpRequest=false;
try {
HttpRequest=new XMLHttpRequest();
} catch(e) {
var arrXMLHTTP=["Msxml3.XMLHTTP","Msxml2.XMLHTTP","Microsoft.XMLHTTP"];
for(var i=0;i<arrXMLHTTP.length;i++) {
try {
HttpRequest=new ActiveXObject(arrXMLHTTP[i]);
} catch(e) {}
if(HttpRequest) break;
}
}
return HttpRequest;
}

//使用ajax
function useAjax(username){
var ajax = creatAjax();
strUrl = "login.php"
ajax.open("POST",strUrl,false);
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
strPost="username="+username;
ajax.send(strPost);
var str = ajax.responseText;
if(str==1){
alert("存在");
}else{
alert("不存在");
}
return false;
}

//PHP 部分
//資料庫連接部分省去
$strSql = "select count(*) as count from usertable where username='".$_POST["username"]."'";

if($row["count"]>0){
echo "1";
exit;
}else{
echo "0";
exit;

}
 

聯繫我們

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