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


Posted in PHP onMarch 29, 2018

本文实例讲述了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" 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"><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" 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"><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>

二. 运行结果

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

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

PHP 相关文章推荐
PHP获得用户使用的代理服务器ip即真实ip
Dec 31 PHP
PHP与SQL注入攻击[二]
Apr 17 PHP
在PHP中使用X-SendFile头让文件下载更快
Jun 01 PHP
php计算函数执行时间的方法
Mar 20 PHP
php文件操作小结(删除指定文件/获取文件夹下的文件名/读取文件夹下图片名)
May 09 PHP
PHP获取指定时间段之间的 年,月,天,时,分,秒
Jun 05 PHP
PHP请求远程地址设置超时时间的解决方法
Oct 29 PHP
php文件上传类的分享
Jul 06 PHP
PHP给源代码加密的几种方法汇总(推荐)
Feb 06 PHP
PHP中16个高危函数整理
Sep 19 PHP
Yii2框架中一些折磨人的坑
Dec 15 PHP
PHP使用PhpSpreadsheet操作Excel实例详解
Mar 26 PHP
PHP使用文件锁解决高并发问题示例
Mar 29 #PHP
PHP实现微信支付(jsapi支付)流程步骤详解
Mar 15 #PHP
PHP实现的数组和XML文件相互转换功能示例
Mar 15 #PHP
PHP实现用户异地登录提醒功能的方法【基于thinkPHP框架】
Mar 15 #PHP
laravel框架关于搜索功能的实现
Mar 15 #PHP
PHP实现浏览器中直接输出图片的方法示例
Mar 14 #PHP
PHP实现生成推广海报的方法详解
Mar 14 #PHP
You might like
php ajax实现文件上传进度条
2016/03/29 PHP
javascript 获取图片颜色
2009/04/05 Javascript
jqPlot jquery的页面图表绘制工具
2009/07/25 Javascript
js判断一个元素是否为另一个元素的子元素的代码
2012/03/21 Javascript
JavaScript实现动态创建CSS样式规则方案
2014/09/06 Javascript
jquery验证邮箱格式是否正确实例讲解
2015/11/16 Javascript
jQuery实现的倒计时效果实例小结
2016/04/16 Javascript
jQuery插件ajaxFileUpload使用实例解析
2016/10/19 Javascript
微信小程序 Windows2008 R2服务器配置TLS1.2方法
2016/12/05 Javascript
详解Vue生命周期的示例
2017/03/10 Javascript
vue vue-Router默认hash模式修改为history需要做的修改详解
2018/09/13 Javascript
详解如何解决vue开发请求数据跨域的问题(基于浏览器的配置解决)
2018/11/12 Javascript
详解webpack 最简打包结果分析
2019/02/20 Javascript
浅谈Node 异步IO和事件循环
2019/05/05 Javascript
JS实现动态添加外部js、css到head标签的方法
2019/06/05 Javascript
深入了解JavaScript代码覆盖
2019/06/13 Javascript
vue使用微信JS-SDK实现分享功能
2019/08/23 Javascript
vuejs实现下拉框菜单选择
2020/10/23 Javascript
微信小程序实现分页加载效果
2020/11/19 Javascript
提升Python程序运行效率的6个方法
2015/03/31 Python
Python使用minidom读写xml的方法
2015/06/03 Python
简单上手Python中装饰器的使用
2015/07/12 Python
解决python3 HTMLTestRunner测试报告中文乱码的问题
2018/12/17 Python
在Python中字符串、列表、元组、字典之间的相互转换
2019/11/15 Python
使用Python 自动生成 Word 文档的教程
2020/02/13 Python
Python如何爬取b站热门视频并导入Excel
2020/08/10 Python
总经理助理岗位职责
2013/11/08 职场文书
初中学生期末评语
2014/04/24 职场文书
《恐龙》教学反思
2014/04/27 职场文书
红领巾广播站广播稿(3篇)
2014/09/20 职场文书
库房管理员岗位职责
2015/02/12 职场文书
慰问信格式
2015/02/14 职场文书
旅游项目合作意向书
2015/05/08 职场文书
2015领导干部廉洁自律工作总结
2015/07/23 职场文书
新年祝酒词大全
2015/08/11 职场文书
springboot使用Redis作缓存使用入门教程
2021/07/25 Redis