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制作静态网站的模板框架(二)
Oct 09 PHP
php 页面执行时间计算代码
Dec 04 PHP
php生成SessionID和图片校验码的思路和实现代码
Mar 10 PHP
PHP中通过语义URL防止网站被攻击的方法分享
Sep 08 PHP
php+mysql查询优化简单实例
Jan 13 PHP
Apache连接PHP后无法启动问题解决思路
Jun 18 PHP
WordPress中转义HTML与过滤链接的相关PHP函数使用解析
Dec 22 PHP
Yii2框架使用计划任务的方法
May 25 PHP
php禁用cookie后session设置方法分析
Oct 19 PHP
php生出随机字符串
Jul 06 PHP
Laravel 5使用Laravel Excel实现Excel/CSV文件导入导出的功能详解
Oct 11 PHP
PHP实现多图上传和单图上传功能
May 17 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
新安装的MySQL数据库需要注意的安全知识
2008/07/30 PHP
php数组函数序列之array_values() 获取数组元素值的函数与方法
2011/10/30 PHP
PHP 日,周,月点击排行统计
2012/01/11 PHP
php 生成短网址原理及代码
2014/01/23 PHP
PHP抓取网页、解析HTML常用的方法总结
2015/07/01 PHP
thinkPHP订单数字提醒功能的实现方法
2016/12/01 PHP
PHP文件操作实例总结【文件上传、下载、分页】
2018/12/08 PHP
PHP+iframe模拟Ajax上传文件功能示例
2019/07/02 PHP
20款超赞的jQuery插件 Web开发人员必备
2011/02/26 Javascript
JS清空多文本框、文本域示例代码
2014/02/24 Javascript
AngularJs验证重复密码的方法(两种)
2016/11/25 Javascript
Bootstrap源码解读按钮(5)
2016/12/23 Javascript
详解vue过滤器在v2.0版本用法
2017/06/01 Javascript
jQuery幻灯片插件owlcarousel参数说明中文文档
2018/02/27 jQuery
Angular中sweetalert弹框的基本使用教程
2018/07/22 Javascript
解决Layui 表单提交数据为空的问题
2018/08/15 Javascript
javascript实现计算指定范围内的质数示例
2018/12/29 Javascript
ssh批量登录并执行命令的python实现代码
2012/05/25 Python
Python字符串匹配算法KMP实例
2015/07/18 Python
Python实现自动登录百度空间的方法
2017/06/10 Python
Python 加密与解密小结
2018/12/06 Python
Python txt文件加入字典并查询的方法
2019/01/15 Python
python使用socket实现的传输demo示例【基于TCP协议】
2019/09/24 Python
python 实现return返回多个值
2019/11/19 Python
TensorFlow 多元函数的极值实例
2020/02/10 Python
jupyter notebook 使用过程中python莫名崩溃的原因及解决方式
2020/04/10 Python
pytorch 计算Parameter和FLOP的操作
2021/03/04 Python
css3的@media属性实现页面响应式布局示例代码
2014/02/10 HTML / CSS
PHP面试题及答案一
2012/06/18 面试题
结婚喜宴家长答谢词
2014/01/15 职场文书
先进工作者获奖感言
2014/02/08 职场文书
餐厅采购员岗位职责
2014/03/06 职场文书
购房意向书
2014/08/30 职场文书
2015元旦家电促销活动策划方案
2014/12/09 职场文书
Html5页面播放M4a音频文件
2021/03/30 HTML / CSS
如何在CocosCreator里画个炫酷的雷达图
2021/04/16 Javascript