一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - PHP教程 - PHP+Ajax實現的博客文章添加類別功能示例

PHP+Ajax實現的博客文章添加類別功能示例

2019-10-14 13:51chengqiuming PHP教程

這篇文章主要介紹了PHP+Ajax實現的博客文章添加類別功能,結合實例形式分析了php+ajax實現的數據交互、數據庫連接、查詢等相關操作技巧,需要的朋友可以參考下

本文實例講述了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程序設計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲精品视频免费在线观看 | 爱爱小说漫画 | 深夜在线网址 | 俄罗斯妈妈k8影院在线观看 | 精品日韩二区三区精品视频 | kayden·kross hd在线 | 热久久免费 | 日本高清中文字幕 | 日本动漫打扑克动画片樱花动漫 | 日韩版码免费福利视频 | 欧美成人一区二区三区 | 手机看片www xiao2b cm | 日本hd18 | 日本五十路六十30人8时间 | 日韩成人免费 | 韩国三级在线播放 | 四虎精品成人免费视频 | 日老逼 | 日韩高清无砖砖区2022 | 超兴奋朋友的中文字幕下 | 日韩欧美国产成人 | 国产91精品露脸国语对白 | 天堂资源8中文最新版 | 日本一卡二卡3卡四卡网站精品 | 色呦呦在线免费观看 | 门房秦大爷在线阅读 | 美国女艳星brandilove | 国产综合成人亚洲区 | 国产福利专区精品视频 | 狠狠的撞击发泄h | 精品精品精品 | 激情小视频网站 | 亚洲欧美日韩在线观看看另类 | 国产精品久久一区 | a4yy欧美一区二区三区 | 亚洲a视频在线 | 国产午夜精品一区二区三区 | 亚州人成网在线播放 | 亚洲一区 在线播放 | 成人精品一区二区三区 | 亚洲欧洲淘宝天堂日本 |