質問編集履歴

1

コードを追加しました

2017/03/18 07:20

投稿

orang2850
orang2850

スコア10

test CHANGED
File without changes
test CHANGED
@@ -6,9 +6,103 @@
6
6
 
7
7
  <?php
8
8
 
9
-
9
+ ob_start();
10
+
11
+ include('config.php');
12
+
13
+ ?>
14
+
15
+ <!DOCTYPE html>
16
+
17
+ <html>
18
+
19
+ <head>
20
+
21
+ <meta charset="utf-8">
22
+
23
+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
24
+
25
+ <title>投票機能の作成方法</title>
26
+
27
+ <script type="text/javascript" src="jquery.js"></script>
10
28
 
11
29
 
30
+
31
+ <script type="text/javascript">
32
+
33
+ $(function() {
34
+
35
+ // buttonがクリックされたときに実行
36
+
37
+ $("input").click(function() {
38
+
39
+ // buttonのIDを取得する
40
+
41
+ var id = $(this).attr("id");
42
+
43
+ // buttonのname(商品名)を取得する
44
+
45
+ var product_name = $(this).attr("name");
46
+
47
+ // POST用のデータ準備:id=をつけないと、vote.phpの$_POST['id']で取得できない
48
+
49
+ var voteData = 'id='+ id;
50
+
51
+ // span内の投票数を書き換える
52
+
53
+ var thisButton = $(this).prev('span');
54
+
55
+ $.ajax({
56
+
57
+ type: "POST",
58
+
59
+ url: "vote.php",
60
+
61
+ data: voteData,
62
+
63
+ success: function(data) {
64
+
65
+ // 処理が成功したら、thisButton内部を書き換える
66
+
67
+ thisButton.html(data);
68
+
69
+ }
70
+
71
+ });
72
+
73
+ return false;
74
+
75
+ });
76
+
77
+ });
78
+
79
+ </script>
80
+
81
+ </head>
82
+
83
+ <body>
84
+
85
+
86
+
87
+ <?php
88
+
89
+ $query = "SELECT * FROM products ORDER BY RAND() LIMIT 2";
90
+
91
+ $result = $mysqli->query($query);
92
+
93
+ while ($row = $result->fetch_assoc()) {
94
+
95
+ $id = $row['id'];
96
+
97
+ $product_name = $row['product_name'];
98
+
99
+ $product_vote = $row['product_vote'];
100
+
101
+ ?>
102
+
103
+
104
+
105
+ <p>
12
106
 
13
107
 
14
108
 
@@ -16,7 +110,13 @@
16
110
 
17
111
  <input type="image" src="<?php echo $product_name;?>" id="<?php echo $id; ?>" name="<?php echo $product_name; ?>"></button>
18
112
 
113
+ </p>
19
114
 
115
+
116
+
117
+ <?php
118
+
119
+ } // End of while
20
120
 
21
121
  ?>
22
122