PHP+Ajax實現部落格文章添加類別功能步驟詳解

來源:互聯網
上載者:User
這次給大家帶來PHP+Ajax實現部落格文章添加類別功能步驟詳解,PHP+Ajax實現部落格文章添加類別功能的注意事項有哪些,下面就是實戰案例,一起來看一下。

一. 代碼

fun.js

function saveType(typename, username){  if(typename==''){   //判斷部落格類別是否為空白    alert('請輸入類別名稱!');  }else if(username==''){   //判斷使用者名稱是否為空白    alert('請輸入使用者名稱!');  }else{    var xmlObj;   //定義XMLHttpRequest對象    var urlData = "typename="+typename+"&username="+username;    //指定要發送的資料    if(window.ActiveXObject){   //判斷瀏覽器是否支援ActiveXObject組件,如支援則通過ActiveXObject方式建立XMLHttpRequest對象      xmlObj = new ActiveXObject("Microsoft.XMLHTTP");    }else if(window.XMLHttpRequest){   //如果瀏覽器不支援ActiveXObject組件,則使用XMLHttpRequest組件建立XMLHttpRequest對象      xmlObj = new XMLHttpRequest();    }    xmlObj.onreadystatechange = callBackFun;   //指定回呼函數    xmlObj.open("POST", "saveType.php", true);   //指定提交方法和頁面    xmlObj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");   //指定發送資料庫類型    xmlObj.send(urlData);   //指定發送的資料    function callBackFun(){   //定義回呼函數      if(xmlObj.readyState == 4 && xmlObj.status == 200){   //如果伺服器端返回內容並無錯誤發生        if(xmlObj.responseText == 'y'){  //判斷儲存是否成功,並給出提示          alert('類別添加成功!');        }else{          alert('類別添加失敗!');        }      }    }  }}

conn.php

<?php$host = '127.0.0.1';$userName = 'root';$password = 'root';$connID = mysql_connect($host, $userName, $password);mysql_select_db('db_database27', $connID);mysql_query('set names gbk');?>

index.php

<?phprequire_once 'conn.php';?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>添加類別</title><link rel="stylesheet" type="text/css" href="css/style.css" rel="external nofollow" rel="external nofollow" /></head><script language="javascript" src="js/fun.js"></script><body><table border="0" align="center" cellpadding="0" cellspacing="0"> <tr>  <td><img src="images/banner.gif" width="608" height="109" /></td> </tr></table><table width="200" height="7" border="0" align="center" cellpadding="0" cellspacing="0"> <tr>  <td></td> </tr></table><table width="608" height="200" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#7ED227"> <tr>  <td width="150" bgcolor="#F6FEFE" valign="top">  <table width="150" border="0" cellpadding="0" cellspacing="1" bordercolor="#FFFFFF" bgcolor="#D6FEFE">   <?php     $sql = mysql_query("select typename,username from tb_type order by addtime desc", $connID);     $result = mysql_fetch_array($sql);     if ($result){       do{   ?>   <tr>    <td bgcolor="#A6E617" height="20"><p align="center"><a href="#" rel="external nofollow" rel="external nofollow" class="a1"><?= $result['typename']?></a></p></td>   </tr>   <tr>    <td bgcolor="#F2F2F2" height="12"><p align="left">發布人:     <?= $result['username']?>     </a></p></td>   </tr>   <?php       }while($result = mysql_fetch_array($sql));     }else{   ?>   <tr>    <td bgcolor="#A6E617" height="20">暫無文章類別</td>   </tr>   <?php     }   ?></table>  </td>  <td width="455" bgcolor="#FFFFFF" style="padding-left:30px; line-height:25px">  請輸入部落格類別:<br />      <input type="text" name="typename" size="30" id="typename" /><br /><br/>  發布人:<br />      <input type="text" name="username" size="30" id="username" /><br /><br/>  <input type="button" value="儲存" onclick="saveType(typename.value, username.value)" />  </td> </tr></table><table width="200" height="7" border="0" align="center" cellpadding="0" cellspacing="0"> <tr>  <td></td> </tr></table><table width="608" height="70" border="0" align="center" cellpadding="0" cellspacing="0"> <tr>  <td background="images/bottom.gif"><p align="center">   <p align="center">『部落格』 著作權 <a href="http://www.mingri***.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="a1">吉林省**科技有限公司</a>!  未經授權禁止複製或建立鏡像!<BR>     <BR>Copyright  <a href="http://www.mingri***.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="a1">www.mingri***.com</a> All Rights Reserved!<BR>   </p>  </p></td> </tr></table></body></html>

saveType.php

<?phprequire_once 'conn.php';?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>添加類別</title><link rel="stylesheet" type="text/css" href="css/style.css" rel="external nofollow" rel="external nofollow" /></head><script language="javascript" src="js/fun.js"></script><body><table border="0" align="center" cellpadding="0" cellspacing="0"> <tr>  <td><img src="images/banner.gif" width="608" height="109" /></td> </tr></table><table width="200" height="7" border="0" align="center" cellpadding="0" cellspacing="0"> <tr>  <td></td> </tr></table><table width="608" height="200" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#7ED227"> <tr>  <td width="150" bgcolor="#F6FEFE" valign="top">  <table width="150" border="0" cellpadding="0" cellspacing="1" bordercolor="#FFFFFF" bgcolor="#D6FEFE">   <?php     $sql = mysql_query("select typename,username from tb_type order by addtime desc", $connID);     $result = mysql_fetch_array($sql);     if ($result){       do{   ?>   <tr>    <td bgcolor="#A6E617" height="20"><p align="center"><a href="#" rel="external nofollow" rel="external nofollow" class="a1"><?= $result['typename']?></a></p></td>   </tr>   <tr>    <td bgcolor="#F2F2F2" height="12"><p align="left">發布人:     <?= $result['username']?>     </a></p></td>   </tr>   <?php       }while($result = mysql_fetch_array($sql));     }else{   ?>   <tr>    <td bgcolor="#A6E617" height="20">暫無文章類別</td>   </tr>   <?php     }   ?></table>  </td>  <td width="455" bgcolor="#FFFFFF" style="padding-left:30px; line-height:25px">  請輸入部落格類別:<br />      <input type="text" name="typename" size="30" id="typename" /><br /><br/>  發布人:<br />      <input type="text" name="username" size="30" id="username" /><br /><br/>  <input type="button" value="儲存" onclick="saveType(typename.value, username.value)" />  </td> </tr></table><table width="200" height="7" border="0" align="center" cellpadding="0" cellspacing="0"> <tr>  <td></td> </tr></table><table width="608" height="70" border="0" align="center" cellpadding="0" cellspacing="0"> <tr>  <td background="images/bottom.gif"><p align="center">   <p align="center">『部落格』 著作權 <a href="http://www.mingri***.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="a1">吉林省**科技有限公司</a>!  未經授權禁止複製或建立鏡像!<BR>     <BR>Copyright  <a href="http://www.mingri***.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="a1">www.mingri***.com</a> All Rights Reserved!<BR>   </p>  </p></td> </tr></table></body></html>

二. 運行結果

相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!

推薦閱讀:

PHP模板方法模式使用詳解

thinkPHP架構自動填滿原理與用法使用詳解

聯繫我們

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