当前位置:Gxlcms > PHP教程 > PHP+Ajax实现的博客文章添加类别功能示例

PHP+Ajax实现的博客文章添加类别功能示例

时间:2021-07-01 10:21:17 帮助过:37人阅读

本文实例讲述了PHP+Ajax实现的博客文章添加类别功能。分享给大家供大家参考,具体如下:

一. 代码

fun.js

  1. function saveType(typename, username){
  2. if(typename==''){ //判断博客类别是否为空
  3. alert('请输入类别名称!');
  4. }else if(username==''){ //判断用户名是否为空
  5. alert('请输入用户名!');
  6. }else{
  7. var xmlObj; //定义XMLHttpRequest对象
  8. var urlData = "typename="+typename+"&username="+username; //指定要发送的数据
  9. if(window.ActiveXObject){ //判断浏览器是否支持ActiveXObject组件,如支持则通过ActiveXObject方式创建XMLHttpRequest对象
  10. xmlObj = new ActiveXObject("Microsoft.XMLHTTP");
  11. }else if(window.XMLHttpRequest){ //如果浏览器不支持ActiveXObject组件,则使用XMLHttpRequest组件创建XMLHttpRequest对象
  12. xmlObj = new XMLHttpRequest();
  13. }
  14. xmlObj.onreadystatechange = callBackFun; //指定回调函数
  15. xmlObj.open("POST", "saveType.php", true); //指定提交方法和页面
  16. xmlObj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;"); //指定发送数据库类型
  17. xmlObj.send(urlData); //指定发送的数据
  18. function callBackFun(){ //定义回调函数
  19. if(xmlObj.readyState == 4 && xmlObj.status == 200){ //如果服务器端返回内容并无错误发生
  20. if(xmlObj.responseText == 'y'){ //判断保存是否成功,并给出提示
  21. alert('类别添加成功!');
  22. }else{
  23. alert('类别添加失败!');
  24. }
  25. }
  26. }
  27. }
  28. }

conn.php

  1. <?php
  2. $host = '127.0.0.1';
  3. $userName = 'root';
  4. $password = 'root';
  5. $connID = mysql_connect($host, $userName, $password);
  6. mysql_select_db('db_database27', $connID);
  7. mysql_query('set names gbk');
  8. ?>

index.php

  1. <?php
  2. require_once 'conn.php';
  3. ?>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  8. <title>添加类别</title>
  9. <link rel="stylesheet" type="text/css" href="css/style.css" rel="external nofollow" rel="external nofollow" />
  10. </head>
  11. <script language="javascript" src="js/fun.js"></script>
  12. <body>
  13. <table border="0" align="center" cellpadding="0" cellspacing="0">
  14. <tr>
  15. <td><img src="images/banner.gif" width="608" height="109" /></td>
  16. </tr>
  17. </table>
  18. <table width="200" height="7" border="0" align="center" cellpadding="0" cellspacing="0">
  19. <tr>
  20. <td></td>
  21. </tr>
  22. </table>
  23. <table width="608" height="200" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#7ED227">
  24. <tr>
  25. <td width="150" bgcolor="#F6FEFE" valign="top">
  26. <table width="150" border="0" cellpadding="0" cellspacing="1" bordercolor="#FFFFFF" bgcolor="#D6FEFE">
  27. <?php
  28. $sql = mysql_query("select typename,username from tb_type order by addtime desc", $connID);
  29. $result = mysql_fetch_array($sql);
  30. if ($result){
  31. do{
  32. ?>
  33. <tr>
  34. <td bgcolor="#A6E617" height="20"><div align="center"><a href="#" rel="external nofollow" rel="external nofollow" class="a1"><?= $result['typename']?></a></div></td>
  35. </tr>
  36. <tr>
  37. <td bgcolor="#F2F2F2" height="12"><div align="left">发布人:
  38. <?= $result['username']?>
  39. </a></div></td>
  40. </tr>
  41. <?php
  42. }while($result = mysql_fetch_array($sql));
  43. }else{
  44. ?>
  45. <tr>
  46. <td bgcolor="#A6E617" height="20">暂无文章类别</td>
  47. </tr>
  48. <?php
  49. }
  50. ?>
  51. </table>
  52. </td>
  53. <td width="455" bgcolor="#FFFFFF" style="padding-left:30px; line-height:25px">
  54. 请输入博客类别:<br />
  55.     <input type="text" name="typename" size="30" id="typename" /><br /><br/>
  56. 发布人:<br />
  57.     <input type="text" name="username" size="30" id="username" /><br /><br/>
  58. <input type="button" value="保存" onclick="saveType(typename.value, username.value)" />
  59. </td>
  60. </tr>
  61. </table>
  62. <table width="200" height="7" border="0" align="center" cellpadding="0" cellspacing="0">
  63. <tr>
  64. <td></td>
  65. </tr>
  66. </table>
  67. <table width="608" height="70" border="0" align="center" cellpadding="0" cellspacing="0">
  68. <tr>
  69. <td background="images/bottom.gif"><div align="center">
  70. <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>
  71. <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>
  72. </DIV>
  73. </div></td>
  74. </tr>
  75. </table>
  76. </body>
  77. </html>

saveType.php

  1. <?php
  2. require_once 'conn.php';
  3. ?>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  8. <title>添加类别</title>
  9. <link rel="stylesheet" type="text/css" href="css/style.css" rel="external nofollow" rel="external nofollow" />
  10. </head>
  11. <script language="javascript" src="js/fun.js"></script>
  12. <body>
  13. <table border="0" align="center" cellpadding="0" cellspacing="0">
  14. <tr>
  15. <td><img src="images/banner.gif" width="608" height="109" /></td>
  16. </tr>
  17. </table>
  18. <table width="200" height="7" border="0" align="center" cellpadding="0" cellspacing="0">
  19. <tr>
  20. <td></td>
  21. </tr>
  22. </table>
  23. <table width="608" height="200" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#7ED227">
  24. <tr>
  25. <td width="150" bgcolor="#F6FEFE" valign="top">
  26. <table width="150" border="0" cellpadding="0" cellspacing="1" bordercolor="#FFFFFF" bgcolor="#D6FEFE">
  27. <?php
  28. $sql = mysql_query("select typename,username from tb_type order by addtime desc", $connID);
  29. $result = mysql_fetch_array($sql);
  30. if ($result){
  31. do{
  32. ?>
  33. <tr>
  34. <td bgcolor="#A6E617" height="20"><div align="center"><a href="#" rel="external nofollow" rel="external nofollow" class="a1"><?= $result['typename']?></a></div></td>
  35. </tr>
  36. <tr>
  37. <td bgcolor="#F2F2F2" height="12"><div align="left">发布人:
  38. <?= $result['username']?>
  39. </a></div></td>
  40. </tr>
  41. <?php
  42. }while($result = mysql_fetch_array($sql));
  43. }else{
  44. ?>
  45. <tr>
  46. <td bgcolor="#A6E617" height="20">暂无文章类别</td>
  47. </tr>
  48. <?php
  49. }
  50. ?>
  51. </table>
  52. </td>
  53. <td width="455" bgcolor="#FFFFFF" style="padding-left:30px; line-height:25px">
  54. 请输入博客类别:<br />
  55.     <input type="text" name="typename" size="30" id="typename" /><br /><br/>
  56. 发布人:<br />
  57.     <input type="text" name="username" size="30" id="username" /><br /><br/>
  58. <input type="button" value="保存" onclick="saveType(typename.value, username.value)" />
  59. </td>
  60. </tr>
  61. </table>
  62. <table width="200" height="7" border="0" align="center" cellpadding="0" cellspacing="0">
  63. <tr>
  64. <td></td>
  65. </tr>
  66. </table>
  67. <table width="608" height="70" border="0" align="center" cellpadding="0" cellspacing="0">
  68. <tr>
  69. <td background="images/bottom.gif"><div align="center">
  70. <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>
  71. <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>
  72. </DIV>
  73. </div></td>
  74. </tr>
  75. </table>
  76. </body>
  77. </html>

二. 运行结果

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP+ajax技巧与应用小结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

人气教程排行