JavaScriptのコーディングをしてブラウザ表示した後に投稿、更新を行おうとするとアラートが表示されるようになりました
function chkchangeのコーディングを行った後に発生したことなので恐らくboolが原因なのではと推測してるんです
boolを用いることで、ブラウザ上でチェックボックスを表示か非表示かの2値をとれているんですが、このコードには何が足りないんでしょうか
PHP
1 $link = mysqli_connect($host, $user, $pass, $dbname) or die("NG"); 2 3 mysqli_select_db($link, "kadai"); 4 5 if (isset($_POST['name'], $_POST['comment'], $_POST['bbs']) && $_POST['bbs'] == "post") { 6 $name = $_POST['name']; 7 $comment = $_POST['comment']; 8 mysqli_query($link, "INSERT INTO XXX (name, comment) VALUES ('$name', '$comment')"); 9 } elseif (isset($_POST['bbs']) && $_POST['bbs'] == "update") { 10 $name = $_POST['name']; 11 $comment = $_POST['comment']; 12 $id = array(); 13 $id = $_POST['chkid']; 14 $id_str = implode(",", $id); 15 mysqli_query($link, "UPDATE XXX SET name = '$name', comment = '$comment' WHERE id in($id_str)"); 16 } elseif (isset($_POST['bbs']) && $_POST['bbs'] == "delete") { 17 $id = array(); 18 $id = $_POST['chkid']; 19 $id_str = implode(",", $id); 20 mysqli_query($link, "DELETE FROM XXX WHERE id in($id_str)"); 21 } 22 23 $result = mysqli_query($link, "SELECT * FROM XXX ORDER BY time DESC"); 24?>
HTML
1<html> 2 <head> 3 <meta charset="utf-8"> 4 <script type="text/javascript"> 5 function condition() { 6 var de = document.getElementById("r3").checked; 7 var up = document.getElementById("r2").checked; 8 if (de == false) { 9 if (document.getElementById('name').value.length < 10) { 10 window.alert("ERROR"); 11 return false; 12 } else if (document.getElementById('name').value.length == "") { 13 window.alert("ERROR"); 14 return false; 15 } else if (document.getElementById('comment').value.length < 300) { 16 window.alert("ERROR"); 17 return false; 18 } else if (document.getElementById('comment').value.length == "") { 19 window.alert("ERROR"); 20 return false; 21 } else if (up == true) { 22 nochk(); 23 } 24 } else { 25 nochk(); 26 } 27 } 28 function change(obj) { 29 console.log(obj.id); 30 if (obj.id == "r1") { 31 document.getElementById("send").value = "投稿"; 32 chkchange(true); 33 } else if (obj.id == "r2") { 34 document.getElementById("send").value = "更新"; 35 chkchange(false); 36 } else if (obj.id == "r3") { 37 document.getElementById("send").value = "削除"; 38 chkchange(false); 39 } 40 } 41 function chkchange(bool){ 42 chk = document.getElementsByName('chkid[]'); 43 for(box = 0; box < chk.length; box++){ 44 console.log(chk[box]); 45 console.log(box); 46 chk[box].disabled = bool; 47 } 48 } 49 if (num == 0) { 50 window.alert("ERROR"); 51 } 52 </script> 53 </head> 54 <body> 55 <form method="post" action="" name="form" onsubmit="return condition()"> 56 <table> 57 <tr> 58 <td> 59 名前:<input type="text" name="name" id="name"> 60 内容:<textarea name="comment" cols="30" rows="3" id="comment"></textarea> 61 <input type="radio" name="bbs" id="r1" value="post" onchange="change(this)">投稿 62 <input type="radio" name="bbs" id="r2" value="update" onchange="change(this)">更新 63 <input type="radio" name="bbs" id="r3" value="delete" onchange="change(this)">削除 64 <input type="submit" value="投稿" id="send"> 65 </td> 66 </tr> 67 </table> 68 69<?php 70 while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { 71?> 72 73 <table> 74 <input type="checkbox" name="chkid[]" value="<?=$row['id'] ?>"> 75 <tr> 76 <td>名前: 77 <?php echo $row['name'] ?> 78 </td> 79 </tr> 80 <tr> 81 <td>内容: 82 <?php echo $row['comment'] ?> 83 </td> 84 </tr> 85 </table> 86 87<?php 88 } 89 mysqli_free_result($result); 90 91 mysqli_close($link); 92?> 93 94 </form> 95 </body> 96</html>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/08/29 07:47