溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

PHP+Ajax如何解實現(xiàn)頁面無刷新發(fā)表評論

發(fā)布時間:2021-07-02 10:54:35 來源:億速云 閱讀:140 作者:小新 欄目:web開發(fā)

小編給大家分享一下PHP+Ajax如何解實現(xiàn)頁面無刷新發(fā)表評論,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

大家都有在網(wǎng)站發(fā)表評論的經(jīng)歷,傳統(tǒng)的發(fā)表過程無非是:發(fā)表->提交頁面表單->等待刷新頁面,這樣在網(wǎng)絡(luò)比較擁擠的時候,往往需要漫長的等待,今天介紹用PHP+Ajax實現(xiàn)頁面無刷新發(fā)表評論,希望對初學(xué)ajax的PHPer有所幫助。   那么首先,我們需要一個基本的ajax開發(fā)框架,文件ajax.js就包含了這個框架,代碼如下:

代碼如下:


var http_request=false;
  function send_request(url){//初始化,指定處理函數(shù),發(fā)送請求的函數(shù)
    http_request=false;
//開始初始化XMLHttpRequest對象
if(window.XMLHttpRequest){//Mozilla瀏覽器
  http_request=new XMLHttpRequest();
  if(http_request.overrideMimeType){//設(shè)置MIME類別
    http_request.overrideMimeType("text/xml");
  }
}
else if(window.ActiveXObject){//IE瀏覽器
  try{
   http_request=new ActiveXObject("Msxml2.XMLHttp");
  }catch(e){
   try{
   http_request=new ActiveXobject("Microsoft.XMLHttp");
   }catch(e){}
  }
    }
if(!http_request){//異常,創(chuàng)建對象實例失敗
  window.alert("創(chuàng)建XMLHttp對象失??!");
  return false;
}
http_request.onreadystatechange=processrequest;
//確定發(fā)送請求方式,URL,及是否同步執(zhí)行下段代碼
    http_request.open("GET",url,true);
http_request.send(null);
  }
  //處理返回信息的函數(shù)
   function processrequest(){
   if(http_request.readyState==4){//判斷對象狀態(tài)
     if(http_request.status==200){//信息已成功返回,開始處理信息
   document.getElementById(reobj).innerHTML=http_request.responseText;
  }
  else{//頁面不正常
   alert("您所請求的頁面不正常!");
  }
   }
  }
   function checkfourm(obj){
    var f=document.fourm;
    var newfourm=f.newfourm.value;
    var username=f.username.value;
    var id=f.id.value;
    if(username==""){
           document.getElementById(obj).innerHTML="<img src=images/false.gif> <font color=red>您必須先登錄!</font>";
     return false;
    }
    else if(newfourm==""){
     document.getElementById(obj).innerHTML="<img src=images/false.gif> <font color=red>您還沒填寫評論內(nèi)容!</font>";
     return false;
    }
    else{
     document.getElementById(obj).innerHTML="正在發(fā)送數(shù)據(jù)...";
     send_request('sendnewfourm.php?username='+username+'&newfourm='+newfourm+'&id='+id);
     reobj=obj;
    }
   }


有一點ajax基礎(chǔ)的通過注釋,應(yīng)該都可以看懂這段代碼,我們可以看出,當(dāng)我們開始發(fā)表評論的時候,在一個特定位置先顯示:正在發(fā)送數(shù)據(jù)...。接著調(diào)用回調(diào)函數(shù)處理數(shù)據(jù)。那么請看服務(wù)器端的代碼:

代碼如下:


<?php
  header('Content-Type:text/html;charset=GB2312');//避免輸出中文亂碼,linux下不需要
  $username=trim($_GET['username']);
  $newfourm=trim($_GET['newfourm']);
  $id=$_GET['id'];
  $time=date("Y-m-d");

  include('inc/config.inc.php');
  include('inc/dbclass.php');
  $db=new db;//從數(shù)據(jù)庫操作類生成實例
  $db->mysql($dbhost,$dbuser,$dbpassword,$dbname);//調(diào)用連接參數(shù)函數(shù)
  $db->createcon();//調(diào)用創(chuàng)建連接函數(shù) 

  $addsql="insert into cr_fourm values(0,'$newfourm','$username','$time',$id)";
  $db->query($addsql);
  echo"<img src=images/pass.gif> <font color=red>評論已成功發(fā)表!</font>";
  //echo $addsql;
  $db->close();//關(guān)閉數(shù)據(jù)庫連接
?>


由于jsvascript采用UTF8編碼,在windows下采用ajax回送服務(wù)器的返回信息就會出現(xiàn)亂碼,因此在win下應(yīng)用開頭第一句是非常必要的。中間那段兩個包含文件是數(shù)據(jù)庫操作類和數(shù)據(jù)庫配置信息,我個人習(xí)慣將基本的數(shù)據(jù)庫操作寫成一個類,方便調(diào)用。到這里相信大家已經(jīng)基本明白這個程序的工作原理了,在給出頁面的HTML代碼:

代碼如下:


<table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td align="center"><?php echo $rows_p[p_info];?></td>
      </tr>
      <tr>
        <td align="center"><br><br><iframe frameborder="0" scrolling="auto" src="showfourm.php?picid=<?=$id;?>" style=HEIGHT:250px;VISIBILITY:inherit;WIDTH:98%;Z-INDEX:2 ></iframe>
</td>
      </tr>
      <tr>
        <td align="center"><br><br>
  <div align="center" id="result"></div>
  <form name="fourm">
  <table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td height="25"> 快速發(fā)表評論<span class="STYLE1">(必須先登陸)用戶名:
                <input name="username" type="text" value="<?=$username?>" readonly>
            </span></td>
          </tr>
          <tr>
            <td height="32" align="center" valign="middle"><textarea name="newfourm" class="f" id="newfourm"></textarea></td>
          </tr>
          <tr>
            <td height="32"> <input name="submit" type="button" value="發(fā)表評論" onClick="checkfourm('result')">
              <input name="reset" type="reset" id="reset" value="重新填寫">
            <input name="id" type="hidden" id="id" value="<?php echo"$id";?>"></td>
          </tr>
        </table>
        </form>
        </td>
      </tr>
    </table>

以上是“PHP+Ajax如何解實現(xiàn)頁面無刷新發(fā)表評論”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI