どなたかご教授下さい!
JavaScript初心者です。
現在sortableを使ってリスト表示しています。
sortableを利用して、liが移動した後に各liのidを取得しようと
以下のScriptでphpファイル①⇒phpファイル②にデータを送信しようとしているのですが、
行った結果、出力(テスト用)されるlogがliのidではなく、Array(文字)ばかりになります。
これは何がおかしいのでしょうか?
レベルの低い質問かと思いますが、よろしくお願い致します。
↓送信元phpファイル①内↓
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>sortable</title> <link href="css/bootstrap.min.css" rel="stylesheet" type="text/css"> <link rel="stylesheet" type="text/css" href="css/style.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script> <script type="text/javascript"src="js/jquery.ui.touch-punch.js"></script> <script src="js/bootstrap.min.js"></script> </head> <body> <ul id="datas"> <li id="1"></li> <li id="2"></li> <li id="3"></li> <li id="4"></li> <li id="5"></li> </ul> <script> $('#datas').sortable({ update: function(){ var log = $(this).sortable("toArray"); for (var i = 0; i < 10; i++){ $.ajax({ type: "post", url: "calc.php", dataType: 'json', data: { "shop_id":log, }, }); }; }}); </script> </body> </html>
↓受信側phpファイル②内↓
<?php //配列を受信する。 $log = $_POST['shop_id']; //ログファイルを開く。(スクリプトと同じディレクトリに出力) $output_file_path = __DIR__ . '/log.txt'; $res_file = fopen($output_file_path , 'a+'); //配列の内容を1個ずつ書き出す。 fwrite($res_file, $log . PHP_EOL); ?>
送信元の"dataType"については有・無や、textなど試しましたが改善がありません。
【補足】
↓の状態であれば、html内に<p id="log"></p>を配置すれば正常にid番号を期待通りの順番で取得できます。
$('#datas').sortable({ update: function(){ var log = $(this).sortable("toArray"); $("#log").text(log); } });
回答3件
あなたの回答
tips
プレビュー