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 相关文章推荐
图片存储与浏览一例(Linux+Apache+PHP+MySQL)
Oct 09 PHP
php的chr和ord函数实现字符加减乘除运算实现代码
Dec 05 PHP
PHP验证码函数代码(简单实用)
Sep 29 PHP
destoon出现验证码不显示时的紧急处理方法
Aug 22 PHP
php生成html文件方法总结
Dec 01 PHP
Yii把CGridView文本框换成下拉框的方法
Dec 03 PHP
php生成年月日下载列表的方法
Apr 24 PHP
php实现求相对时间函数
Jun 15 PHP
PHP 实现页面静态化的几种方法
Jul 23 PHP
PHP调用API接口实现天气查询功能的示例
Sep 21 PHP
php报错502badgateway解决方法
Oct 11 PHP
ThinkPHP5与单元测试PHPUnit使用详解
Feb 23 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 HandlerSocket的使用
2011/05/02 PHP
超级实用的7个PHP代码片段分享
2012/01/05 PHP
Uncaught exception com_exception with message Failed to create COM object
2012/01/11 PHP
用php简单实现加减乘除计算器
2014/01/06 PHP
初识通用数据库操作类――前端easyui-datagrid,form(php)
2015/07/31 PHP
php用户注册信息验证正则表达式
2015/11/12 PHP
利用PHPExcel读取Excel的数据和导出数据到Excel
2017/05/12 PHP
PHP实现基于回溯法求解迷宫问题的方法详解
2017/08/17 PHP
PHP实现的各类hash算法长度及性能测试实例
2017/08/27 PHP
基于swoole实现多人聊天室
2018/06/14 PHP
Laravel 自动转换长整型雪花 ID 为字符串的实现
2020/10/27 PHP
JS打印gridview实现原理及代码
2013/02/05 Javascript
js实现表格字段排序
2014/02/19 Javascript
JavaScript框架是什么?怎样才能叫做框架?
2015/07/01 Javascript
15款最好的Bootstrap在线编辑器
2016/08/03 Javascript
使用jQuery实现动态添加小广告
2017/07/11 jQuery
jQuery 获取除某指定对象外的其他对象 ( :not() 与.not())
2018/10/10 jQuery
其实你可以少写点if else与switch(推荐)
2019/01/10 Javascript
Python 专题三 字符串的基础知识
2017/03/19 Python
Python实现简单过滤文本段的方法
2017/05/24 Python
Python简单实现自动删除目录下空文件夹的方法
2017/08/29 Python
Python在图片中插入大量文字并且自动换行
2019/01/02 Python
Django数据库类库MySQLdb使用详解
2019/04/28 Python
python装饰器相当于函数的调用方式
2019/12/27 Python
Python如何基于smtplib发不同格式的邮件
2019/12/30 Python
python3实现raspberry pi(树莓派)4驱小车控制程序
2020/02/12 Python
肯尼亚网上商城:Kilimall
2016/08/20 全球购物
印尼最大的婴儿用品购物网站:Orami
2017/09/28 全球购物
菲律宾优惠券网站:MetroDeal
2019/04/12 全球购物
国际会计专业求职信
2014/08/04 职场文书
2014年幼儿园工作总结
2014/11/10 职场文书
求职自荐信怎么写
2015/03/04 职场文书
公司人事任命通知
2015/04/20 职场文书
投标售后服务承诺书
2015/04/29 职场文书
冲出亚马逊观后感
2015/06/03 职场文书
Vue的过滤器你真了解吗
2022/02/24 Vue.js