回答編集履歴
1
追記
test
CHANGED
@@ -3,3 +3,127 @@
|
|
3
3
|
|
4
4
|
|
5
5
|
javascript を使って、ajax でPHPにパラメータを渡し、レスポンスを受け取って表示します。
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
---
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
とりあえず、jQuery を使って Ajax を使うサンプルです。
|
14
|
+
|
15
|
+
基本的な Ajax のコードの動きをしっかり把握することから始め、メールフォームへと改変していってください。
|
16
|
+
|
17
|
+
index.html のフォームから、test.php に値を渡し、phpでの処理結果を index.html に返却しているコードです。
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
```php
|
22
|
+
|
23
|
+
<!-- index.html -->
|
24
|
+
|
25
|
+
<!DOCTYPE HTML>
|
26
|
+
|
27
|
+
<html lang="ja">
|
28
|
+
|
29
|
+
<head>
|
30
|
+
|
31
|
+
<meta charset="UTF-8">
|
32
|
+
|
33
|
+
<title></title>
|
34
|
+
|
35
|
+
</head>
|
36
|
+
|
37
|
+
<body>
|
38
|
+
|
39
|
+
<div>
|
40
|
+
|
41
|
+
<form action="" method="post">
|
42
|
+
|
43
|
+
<p>
|
44
|
+
|
45
|
+
<input type="number" name="num" />
|
46
|
+
|
47
|
+
</p>
|
48
|
+
|
49
|
+
<p>
|
50
|
+
|
51
|
+
<button type="button">計算</button>
|
52
|
+
|
53
|
+
</p>
|
54
|
+
|
55
|
+
<p>
|
56
|
+
|
57
|
+
結果: <span id="result"></span>
|
58
|
+
|
59
|
+
</p>
|
60
|
+
|
61
|
+
</form>
|
62
|
+
|
63
|
+
</div>
|
64
|
+
|
65
|
+
<script type="text/javascript" src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
|
66
|
+
|
67
|
+
<script type="text/javascript">
|
68
|
+
|
69
|
+
$(function () {
|
70
|
+
|
71
|
+
$('button').on('click', function () {
|
72
|
+
|
73
|
+
$.ajax({
|
74
|
+
|
75
|
+
url: 'test.php'
|
76
|
+
|
77
|
+
, method: 'post'
|
78
|
+
|
79
|
+
, dataType: 'json'
|
80
|
+
|
81
|
+
, data: $('form').serialize()
|
82
|
+
|
83
|
+
, success: function (json) {
|
84
|
+
|
85
|
+
$('#result').text(json.res);
|
86
|
+
|
87
|
+
}
|
88
|
+
|
89
|
+
});
|
90
|
+
|
91
|
+
});
|
92
|
+
|
93
|
+
});
|
94
|
+
|
95
|
+
</script>
|
96
|
+
|
97
|
+
</body>
|
98
|
+
|
99
|
+
</html>
|
100
|
+
|
101
|
+
```
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
```php
|
106
|
+
|
107
|
+
<?php
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
/**
|
112
|
+
|
113
|
+
* test.php
|
114
|
+
|
115
|
+
*
|
116
|
+
|
117
|
+
* @since 2018/09/30
|
118
|
+
|
119
|
+
*/
|
120
|
+
|
121
|
+
header('Content-type: application/json');
|
122
|
+
|
123
|
+
$num = filter_input(INPUT_POST, 'num');
|
124
|
+
|
125
|
+
echo json_encode(['res' => $num * 2]);
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
```
|