ホームページ > バックエンド開発 > PHPチュートリアル > PHP+Ajaxを使ってブログ記事にカテゴリーを追加する機能を実装する手順を詳しく解説

PHP+Ajaxを使ってブログ記事にカテゴリーを追加する機能を実装する手順を詳しく解説

php中世界最好的语言
リリース: 2023-03-26 10:16:02
オリジナル
2332 人が閲覧しました

今回は、PHP+Ajaxでブログ記事にカテゴリーを追加する機能を実装する手順について、PHP+Ajaxを使って実装する際の注意点について詳しく解説していきます。ブログ投稿にカテゴリを追加する機能 以下に実際のケースを見てみましょう。

1. コード

fun.js

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

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

1

2

3

4

5

6

7

8

<?php

$host = &#39;127.0.0.1&#39;;

$userName = &#39;root&#39;;

$password = &#39;root&#39;;

$connID = mysql_connect($host, $userName, $password);

mysql_select_db(&#39;db_database27&#39;, $connID);

mysql_query(&#39;set names gbk&#39;);

?>

ログイン後にコピー

index.php

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

<?php

require_once &#39;conn.php&#39;;

?>

<!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"><p align="center"><a href="#" rel="external nofollow" rel="external nofollow" class="a1"><?= $result[&#39;typename&#39;]?></a></p></td>

   </tr>

   <tr>

    <td bgcolor="#F2F2F2" height="12"><p align="left">发布人:

     <?= $result[&#39;username&#39;]?>

     </a></p></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"><p align="center">

   <p 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>

   </p>

  </p></td>

 </tr>

</table>

</body>

</html>

ログイン後にコピー
ログイン後にコピー

saveType.php

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

<?php

require_once &#39;conn.php&#39;;

?>

<!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"><p align="center"><a href="#" rel="external nofollow" rel="external nofollow" class="a1"><?= $result[&#39;typename&#39;]?></a></p></td>

   </tr>

   <tr>

    <td bgcolor="#F2F2F2" height="12"><p align="left">发布人:

     <?= $result[&#39;username&#39;]?>

     </a></p></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"><p align="center">

   <p 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>

   </p>

  </p></td>

 </tr>

</table>

</body>

</html>

ログイン後にコピー
ログイン後にコピー

2.

信じてくださいこの記事の事例を読んだ後は、その方法をマスターしたことになります。さらに興味深い情報については、PHP 中国語 Web サイトの他の関連記事に注目してください。

推奨読書:

PHPテンプレートメソッドパターンの使用方法の詳細な説明


thinkPHPフレームワークの自動入力原理と使用法の詳細な説明

以上がPHP+Ajaxを使ってブログ記事にカテゴリーを追加する機能を実装する手順を詳しく解説の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート