您好,登錄后才能下訂單哦!
小編給大家分享一下PHP+Ajax如何實(shí)現(xiàn)的博客文章添加類別功能,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
具體如下:
一. 代碼
fun.js
function saveType(typename, username){ if(typename==''){ //判斷博客類別是否為空 alert('請(qǐng)輸入類別名稱!'); }else if(username==''){ //判斷用戶名是否為空 alert('請(qǐng)輸入用戶名!'); }else{ var xmlObj; //定義XMLHttpRequest對(duì)象 var urlData = "typename="+typename+"&username="+username; //指定要發(fā)送的數(shù)據(jù) if(window.ActiveXObject){ //判斷瀏覽器是否支持ActiveXObject組件,如支持則通過(guò)ActiveXObject方式創(chuàng)建XMLHttpRequest對(duì)象 xmlObj = new ActiveXObject("Microsoft.XMLHTTP"); }else if(window.XMLHttpRequest){ //如果瀏覽器不支持ActiveXObject組件,則使用XMLHttpRequest組件創(chuàng)建XMLHttpRequest對(duì)象 xmlObj = new XMLHttpRequest(); } xmlObj.onreadystatechange = callBackFun; //指定回調(diào)函數(shù) xmlObj.open("POST", "saveType.php", true); //指定提交方法和頁(yè)面 xmlObj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;"); //指定發(fā)送數(shù)據(jù)庫(kù)類型 xmlObj.send(urlData); //指定發(fā)送的數(shù)據(jù) function callBackFun(){ //定義回調(diào)函數(shù) if(xmlObj.readyState == 4 && xmlObj.status == 200){ //如果服務(wù)器端返回內(nèi)容并無(wú)錯(cuò)誤發(fā)生 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
<?php require_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"><div align="center"><a href="#" rel="external nofollow" rel="external nofollow" class="a1"><?= $result['typename']?></a></div></td> </tr> <tr> <td bgcolor="#F2F2F2" height="12"><div align="left">發(fā)布人: <?= $result['username']?> </a></div></td> </tr> <?php }while($result = mysql_fetch_array($sql)); }else{ ?> <tr> <td bgcolor="#A6E617" height="20">暫無(wú)文章類別</td> </tr> <?php } ?> </table> </td> <td width="455" bgcolor="#FFFFFF" > 請(qǐng)輸入博客類別:<br /> <input type="text" name="typename" size="30" id="typename" /><br /><br/> 發(fā)布人:<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"><div align="center"> <DIV align="center">『博客』 版權(quán)所有 <a href="http://www.mingri***.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="a1">吉林省**科技有限公司</a>! 未經(jīng)授權(quán)禁止復(fù)制或建立鏡像!<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> </DIV> </div></td> </tr> </table> </body> </html>
saveType.php
<?php require_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"><div align="center"><a href="#" rel="external nofollow" rel="external nofollow" class="a1"><?= $result['typename']?></a></div></td> </tr> <tr> <td bgcolor="#F2F2F2" height="12"><div align="left">發(fā)布人: <?= $result['username']?> </a></div></td> </tr> <?php }while($result = mysql_fetch_array($sql)); }else{ ?> <tr> <td bgcolor="#A6E617" height="20">暫無(wú)文章類別</td> </tr> <?php } ?> </table> </td> <td width="455" bgcolor="#FFFFFF" > 請(qǐng)輸入博客類別:<br /> <input type="text" name="typename" size="30" id="typename" /><br /><br/> 發(fā)布人:<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"><div align="center"> <DIV align="center">『博客』 版權(quán)所有 <a href="http://www.mingri***.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="a1">吉林省**科技有限公司</a>! 未經(jīng)授權(quán)禁止復(fù)制或建立鏡像!<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> </DIV> </div></td> </tr> </table> </body> </html>
二. 運(yùn)行結(jié)果
一、phpStudy,是一個(gè)新手入門最常用的開發(fā)環(huán)境。二、WampServer,WampServer也同樣的也是和phpStudy一樣操作簡(jiǎn)單對(duì)小白比較友好。三、XAMPP,XAMPP(Apache+MySQL+PHP+PERL)是一個(gè)功能強(qiáng)大的建站集成軟件包;四、MAMP,MAMP分為兩種MAMP和MAMP Pro for Mac。五、寶塔面板,寶塔面板是一款服務(wù)器管理軟件,支持windows和linux系統(tǒng)。六、UPUPW,UPUPW是目前Windows平臺(tái)下最具特色的Web服務(wù)器PHP套件。
以上是“PHP+Ajax如何實(shí)現(xiàn)的博客文章添加類別功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。