jQueryを使ったコーディングで画面遷移を行いたい時に、Ajaxを使わないで処理することって可能なんでしょうか
PHP
1<?php 2 header("Content-Type: application/json; charset=UTF-8");//~json;だとAjax通信になってしまうので、ここも変更したい 3 4 if ($_POST['bbs'] == "post") { 5 require ("XXX1.php");//各~.phpへ異なる動作・遷移の処理を行う こちらでは投稿処理 6 $p_obj = new Post(); 7 $p_obj -> name = $_POST['name']; 8 $p_obj -> comment = $_POST['comment']; 9 $p_obj -> post(); 10 } elseif ($_POST['bbs'] == "update") { 11 require ("XXX2.php");//各~.phpへ異なる動作・遷移の処理を行う こちらでは更新処理 12 $u_obj = new Update(); 13 $u_obj -> name = $_POST['name']; 14 $u_obj -> comment = $_POST['comment']; 15 $u_obj -> chk = $_POST['chk']; 16 $u_obj -> update(); 17 } elseif ($_POST['bbs'] == "delete") { 18 require ("XXX3.php");//各~.phpへ異なる動作・遷移の処理を行う こちらでは削除処理 19 $d_obj = new Delete(); 20 $d_obj -> chk = $_POST['chk']; 21 $d_obj -> delete(); 22 } 23 24 require("XXX4.php");//各~.phpへ異なる動作・遷移の処理を行う こちらでは投稿した値の一覧を取得する処理 25 $l_obj = new List_table; 26 $data = $l_obj -> list(); 27 echo json_encode($data); //←ここの文字列を返すコードも変更したい 28 exit; 29?>
<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
プレビュー