jQueryを使ったコーディングで画面遷移を行いたい時に、Ajaxを使わないで処理することって可能なんでしょうか
また、application/json;とjson_encodeについてなんですが、この2つのコードはAjax通信を行う際に使用するコードで、jsonで受信した値をjson_encodeを使ってjson形式で文字列を返すという処理を行っているという解釈で合ってるのでしょうか
PHP
1//a.php 2<?php 3 header("Content-Type: application/json; charset=UTF-8");//~json;だとAjax通信になってしまうので、ここも変更したい 4 5 if ($_POST['bbs'] == "post") { 6 require ("XXX1.php");//各~.phpへ異なる動作・遷移の処理を行う こちらでは投稿処理 7 $p_obj = new Post(); 8 $p_obj -> name = $_POST['name']; 9 $p_obj -> comment = $_POST['comment']; 10 $p_obj -> post(); 11 } elseif ($_POST['bbs'] == "update") { 12 require ("XXX2.php");//各~.phpへ異なる動作・遷移の処理を行う こちらでは更新処理 13 $u_obj = new Update(); 14 $u_obj -> name = $_POST['name']; 15 $u_obj -> comment = $_POST['comment']; 16 $u_obj -> chk = $_POST['chk']; 17 $u_obj -> update(); 18 } elseif ($_POST['bbs'] == "delete") { 19 require ("XXX3.php");//各~.phpへ異なる動作・遷移の処理を行う こちらでは削除処理 20 $d_obj = new Delete(); 21 $d_obj -> chk = $_POST['chk']; 22 $d_obj -> delete(); 23 } 24 25 require("XXX4.php");//各~.phpへ異なる動作・遷移の処理を行う こちらでは投稿した値の一覧を取得する処理 26 $l_obj = new List_table; 27 $data = $l_obj -> list(); 28 echo json_encode($data); //←ここの文字列を返すコードも変更したい 29 exit; 30?>
//b.php <html> <head> <meta charset="utf-8"> <meta http-equiv="Content-Script-Type" content="text/javascript"> //下記jQueryの処理とフォームでの処理をa.phpへ送信したい <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script> $(function() { $('input[name=bbs]').click(function(e) { change($(this).prop('id')); }); change($('input[name=bbs]:checked').prop('id')); }); function condition() { if ($('#r1').prop('checked')) { with($('#name')) { if (val().length >= 10) { alert("ERROR"); return false; } if (val() === "") { alert("ERROR"); return false; } } with($('#comment')) { if (val().length >= 300) { alert("ERROR"); return false; } if (val() === "") { alert("ERROR"); return false; } } } if($('#r2').prop('checked')||$('#r3').prop('checked')) { if ($('[name="chkid[]"]:checked').length == 0) { window.alert("ERROR"); return false; } } return true; } function change(myid) { var sendlist = { "r1":{"text":"投稿",checkflg:true}, "r2":{"text":"更新",checkflg:false}, "r3":{"text":"削除",checkflg:false}, }; $('#send').val(sendlist[myid].text); $('[name="chkid[]"]').prop('disabled',sendlist[myid].checkflg); } </script> </head> <body> //フォーム入力と、各ボタン操作を行う <form method="post" action="a.php" onsubmit="return condition()"> <table> <tr> <td> 名前:<input type="text" name="name" id="name"> 内容:<textarea name="comment" cols="30" rows="3" id="comment"></textarea> <input type="radio" name="bbs" id="r1" value="post" onChange="change(this)">投稿 <input type="radio" name="bbs" id="r2" value="update" onChange="change(this)">更新 <input type="radio" name="bbs" id="r3" value="delete" onChange="change(this)">削除 <input type="submit" value="投稿" id="send"> </td> </tr> </table> <?php $link = mysqli_connect($host, $user, $pass, $dbname) or die("NG"); mysqli_select_db($link, "xxx"); $result = mysqli_query($link, "SELECT * FROM xxx ORDER BY time DESC"); while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { ?> <table> <input type="checkbox" name="chkid[]" value="<?=$row['id'] ?>"> <tr> <td>名前: <?php echo $row['name'] ?> </td> </tr> <tr> <td>内容: <?php echo $row['comment'] ?> </td> </tr> </table> <?php } mysqli_free_result($result); mysqli_close($link); ?> </form> </body> </html>
回答1件
あなたの回答
tips
プレビュー