jQueryとPHPでAjaxを利用して入力フォームを作っています。
PHP側で入力データがnullで返ってくるので困っています。
ググって調べたのを試しましたが、いずれもnullが返ってきました。
こんな感じで入力値がnullで出力されてしまいます。
{"id":null,"hoge":"hogehoge"}
追記(2022-08-04)
Javascript側のconsole.logではnullが出力されませんが、PHPの方ではnullが表示されてしまいます。
PHPにjsonデータだけ出力して表示したいということです。
ソースコードは以下の通りです。
html
<html> <head> <meta charset="UTF-8"> <title>入力フォーム</title> </head> <body> <form id="bitobopass_table" name="bitobopass_table"> <table id="passtable"> <tr> <td><input id="twid" name="twid" type="text" placeholder="ID"></td> </tr> </table> <button type="button" id="sendBtn" name="sendBtn">送信</button> </form> <table id="export01"> <tr> <td>id</td> <td>pass</td> </tr> </table> <script src="https://code.jquery.com/jquery-3.6.0.js"></script> <script src="js/receive.js"></script> </body> </html>
javascript
$(function() { $('#sendBtn').on('click', function(event) { let id = $('#twid').val(); $.ajax({ type: "POST", url: "../request.php", data: { "id" : id }, dataType : "json" }).done(function(){ console.log('success'); }).fail(function(){ console.log('fail'); }); }); });
php
<?php $id = $_POST['twid']; $list = array("id" => $id, "hoge" => "hogehoge"); header("Content-type: application/json; charset=UTF-8"); echo json_encode($list); exit; ?>
フォルダ階層
鯖┬js─receive.js
├css
├request.php
└top.html
動作環境
Google Chrome 103.0.5060.134
レンタル鯖(xfree)
FTPソフト(Filezilla)
・nullになる原因は何か
・どのようにすればnullが解決できるか
ご回答いただけると嬉しいです。
まだ回答がついていません
会員登録して回答してみよう