回答編集履歴

1

成功したソースの追加

2018/07/27 12:41

投稿

sonohe
sonohe

スコア6

test CHANGED
@@ -15,3 +15,97 @@
15
15
 
16
16
 
17
17
  上記のurlのhoge.phpをhoge.php?request=3124にしたところ成功しました。
18
+
19
+
20
+
21
+ [index.php]
22
+
23
+ <!DOCTYPE html>
24
+
25
+ <html lang="ja">
26
+
27
+ <head>
28
+
29
+ <meta charset="utf-8" />
30
+
31
+ <title></title>
32
+
33
+ <script src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
34
+
35
+ <script>
36
+
37
+ $(document).ready(function() {
38
+
39
+ $('#send').click(function() {
40
+
41
+ $.ajax({
42
+
43
+ type: "GET",
44
+
45
+ url: "hoge.php?request=3124",
46
+
47
+ }).success(function(data, dataType) {
48
+
49
+ // PHPから返ってきたデータの表示
50
+
51
+ alert(data);
52
+
53
+ }).error(function(XMLHttpRequest, textStatus, errorThrown) {
54
+
55
+ alert('Error : ' + errorThrown);
56
+
57
+ });
58
+
59
+ return false;
60
+
61
+ });
62
+
63
+ });
64
+
65
+ </script>
66
+
67
+ </head>
68
+
69
+ <body>
70
+
71
+ <a href="" id="send">お気に入り</a>
72
+
73
+ </body>
74
+
75
+ </html>
76
+
77
+
78
+
79
+
80
+
81
+ [hoge.php]
82
+
83
+ <?php
84
+
85
+
86
+
87
+ header('Content-type: text/plain; charset=UTF-8');
88
+
89
+
90
+
91
+ if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
92
+
93
+ // Ajaxリクエストの場合のみ処理する
94
+
95
+
96
+
97
+ if (isset($_GET['request'])) {
98
+
99
+ //ここに何かしらの処理を書く(DB登録やファイルへの書き込みなど)
100
+
101
+ echo '成功';
102
+
103
+ } else {
104
+
105
+ echo '失敗';
106
+
107
+ }
108
+
109
+ }
110
+
111
+ ?>