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 相关文章推荐
多文件上载系统完整版
Oct 09 PHP
通过PHP CLI实现简单的数据库实时监控调度
Jul 01 PHP
PHP缓存技术的多种方法小结
Aug 14 PHP
PHP捕获Fatal error错误的方法
Jun 11 PHP
PHP连接Nginx服务器并解析Nginx日志的方法
Aug 16 PHP
PHP的Yii框架中行为的定义与绑定方法讲解
Mar 18 PHP
PHP开发中解决并发问题的几种实现方法分析
Nov 13 PHP
详解php语言最牛掰的Laravel框架
Nov 20 PHP
Ajax+Jpgraph实现的动态折线图功能示例
Feb 11 PHP
PHP面向对象程序设计之对象的遍历操作示例
Jun 12 PHP
基于PHP实现微信小程序客服消息功能
Aug 12 PHP
PHP读取文件或采集时解决中文乱码
Mar 09 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中将图片gif,jpg或mysql longblob或blob字段值转换成16进制字符串
2011/08/23 PHP
深入理解curl类,可用于模拟get,post和curl下载
2013/06/08 PHP
PHP查找与搜索数组元素方法总结
2015/06/12 PHP
使用PHPStorm+XDebug搭建单步调试环境
2017/11/19 PHP
JAVASCRIPT  THIS详解 面向对象
2009/03/25 Javascript
Javascript 判断函数类型完美解决方案
2009/09/02 Javascript
javascript function调用时的参数检测常用办法
2010/02/26 Javascript
JavaScript实现多维数组的方法
2013/11/20 Javascript
js实现有时间限制消失的图片方法
2015/02/27 Javascript
浅谈JavaScript中setInterval和setTimeout的使用问题
2015/08/01 Javascript
jQuery AjaxUpload 上传图片代码
2016/02/02 Javascript
JavaScript中获取纯正的undefined的方法
2016/03/06 Javascript
JS实现图片上传多次上传同一张不生效的处理方法
2018/08/06 Javascript
30分钟快速实现小程序语音识别功能
2018/11/27 Javascript
使用vue实现多规格选择实例(SKU)
2019/08/23 Javascript
vue实现编辑器键盘抬起时内容跟随光标距顶位置向上滚动效果
2020/05/28 Javascript
vue项目在webpack2实现移动端字体自适配功能
2020/06/02 Javascript
[03:27]最受玩家喜爱奖提名:PZH_Element 致玩家寄语
2016/12/20 DOTA
Python MD5加密实例详解
2017/08/02 Python
Python实现返回数组中第i小元素的方法示例
2017/12/04 Python
对python使用http、https代理的实例讲解
2018/05/07 Python
python生成每日报表数据(Excel)并邮件发送的实例
2019/02/03 Python
深入了解Python iter() 方法的用法
2019/07/11 Python
Python装饰器使用你可能不知道的几种姿势
2019/10/25 Python
Python如何实现Paramiko的二次封装
2021/01/30 Python
用html5的canvas画布绘制贝塞尔曲线完整代码
2013/08/14 HTML / CSS
Alba Moda德国网上商店:意大利时尚女装销售
2016/11/14 全球购物
英国领先的杂志订阅网站:Magazine.co.uk
2018/01/25 全球购物
十佳美德少年事迹材料
2014/02/05 职场文书
十八届三中全会宣传方案
2014/02/21 职场文书
关于观后感的作文
2015/06/18 职场文书
2015年酒店销售部工作总结
2015/07/24 职场文书
预备党员入党感言
2015/08/01 职场文书
幼儿园园长新年寄语
2015/08/17 职场文书
自荐信大全
2019/03/21 职场文书
python小型的音频操作库mp3Play
2022/04/24 Python