欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-02-20 11:06:42
本文实例讲述了php+ajax实现的博客文章添加类别功能。分享给大家供大家参考,具体如下: 一. 代码 fun.js function savetype(ty...

本文实例讲述了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+ajax技巧与应用小结》、《php网络编程技巧总结》、《php基本语法入门教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

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