teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

動作確認内容を追記

2018/03/10 02:16

投稿

nakazawaken1
nakazawaken1

スコア94

answer CHANGED
@@ -1,4 +1,82 @@
1
1
  var post_data = {'com' : var name.val()};
2
2
 
3
3
  var post_data = {'com' : name};
4
- ではないでしょうか。
4
+ ではないでしょうか。
5
+
6
+ ■追記■
7
+ 次のファイルで動作確認してみましたが正常にPOSTされているようです。
8
+ [post.php]
9
+ ```php
10
+ <?php
11
+ #入力内容をファイルに記録
12
+ error_log(date(DATE_ATOM) . ' ' . $_REQUEST['com'] . "\r\n", 3, '/tmp/post.php.log');
13
+ ?>ok
14
+ ```
15
+
16
+ [post.html]
17
+ ```html
18
+ <html>
19
+ <head>
20
+ <script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>
21
+ <script type="text/javascript">
22
+ jQuery(function($) {
23
+ $('#send').submit(function(){
24
+ var name = $('#com').val();
25
+ if (name.length === 0){
26
+ return false;
27
+ } else {
28
+ var post_data = {'com' : name};
29
+ $.ajax({
30
+ type: 'POST',
31
+ url: 'post.php',
32
+ data: post_data,
33
+ });
34
+ return false;
35
+ };
36
+ });
37
+ });
38
+ </script>
39
+ </head>
40
+ <body>
41
+ <form id="send">
42
+ <input name="com" type="text" id="com" class="text" maxlength="30"/>
43
+ <input type="submit" class="submit" value="投稿"/>
44
+ </form>
45
+ </body>
46
+ </html>
47
+ ```
48
+
49
+ [post.htmlを開いて入力]
50
+ ![入力](2f9359b151c60f423277df81b293b395.jpeg)
51
+
52
+ [/tmp/post.php.log]
53
+ ```
54
+ 2018-03-10T00:34:28+00:00 aaa
55
+
56
+ ```
57
+
58
+ どうしてもだめそうなら以下のコードで試してみてください。
59
+ [post.html修正版]
60
+ ```html
61
+ <html>
62
+ <head>
63
+ <script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>
64
+ <script type="text/javascript">
65
+ jQuery(function($) {
66
+ $('#send').on('click', function(){
67
+ var name = $('#com').val();
68
+ if (name.length > 0){
69
+ $.post('post.php', {com : name});
70
+ };
71
+ });
72
+ });
73
+ </script>
74
+ </head>
75
+ <body>
76
+ <form>
77
+ <input type="text" id="com" class="text" maxlength="30"/>
78
+ <input type="button" id="send" class="submit" value="投稿"/>
79
+ </form>
80
+ </body>
81
+ </html>
82
+ ```