小编给大家分享一下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
<?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">发布人: <?= $result['username']?> </a></div></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" > 请输入博客类别:<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"><div align="center"> <DIV 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> </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">发布人: <?= $result['username']?> </a></div></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" > 请输入博客类别:<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"><div align="center"> <DIV 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> </DIV> </div></td> </tr> </table> </body> </html>
二. 运行结果
一、phpStudy,是一个新手入门最常用的开发环境。二、WampServer,WampServer也同样的也是和phpStudy一样操作简单对小白比较友好。三、XAMPP,XAMPP(Apache+MySQL+PHP+PERL)是一个功能强大的建站集成软件包;四、MAMP,MAMP分为两种MAMP和MAMP Pro for Mac。五、宝塔面板,宝塔面板是一款服务器管理软件,支持windows和linux系统。六、UPUPW,UPUPW是目前Windows平台下最具特色的Web服务器PHP套件。
以上是“PHP+Ajax如何实现的博客文章添加类别功能”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。