jushiro Posted October 12, 2011 Share Posted October 12, 2011 Is is possible to give multiple values in a single checkbox? i need to have a checkbox that holds multiple values. Help Anyone.. Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted October 12, 2011 Share Posted October 12, 2011 No. A checkbox is either on or off, two possibilities. If it is off it will not be submitted and you need to detect that and assign a value based upon its non-existence. If you mean something slightly different then you may do something like this but it is a hack: <input type="checkbox" name="cb1" value="value1:value2"> Then in PHP you would do: $multiple_values = explode(':', $_POST['cb1']); And you will have an array of multiple values. Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 12, 2011 Share Posted October 12, 2011 As AbraCadaver stated a checkbox is either checked or not and it only has one value. But, if the fact the checkbox is supposed represent multiple selections, then you could handle that on the server-side completely. For example, let's say you have the following checkboxes Pick a Color: <input type="checkbox" name="all_colors" value="all"> All <input type="checkbox" name="colors[]" value="blue"> Blue <input type="checkbox" name="colors[]" value="red"> Red <input type="checkbox" name="colors[]" value="yellow"> Yellow <input type="checkbox" name="colors[]" value="green"> Green You could do something like this if(isset($_POST['all_colors'])) { $user_colors = array('blue', 'red', 'yellow', 'green'); } else { $user_colors = isset($_POST['all_colors']) ? $_POST['all_colors'] : array(); } Not exactly the most elegant solution, but it could improved based upon your specific need. Quote Link to comment Share on other sites More sharing options...
jushiro Posted October 13, 2011 Author Share Posted October 13, 2011 hmm thx for the kind reply guys. .. the reason why i want to have a checkbox of multiple value because.. i want to have a form with a checkbox that reflects values on database.. then when a checkbox is checked then the checked values will be place again in the database in a different table.. so i used INSERT INTO for that.. Here look at my code. hope you could gelp me. <?php <form name="formapprove" method="POST" action="valval3.php" > <div id="apDiv12"><input type="image" src="images/Approve.png" name="approve" value="approve" width="170" height="35" border="0" > </div> <div id="apDiv11"> <input type="image" src="images/reject.png" name="reject" width="170" height="35" border="0" value="reject"> </div> <div id="apDiv14">'; $host="localhost"; $username="root"; $password=""; $db_name="dbreg"; $tbl_name="account"; mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql); echo "<table border=\"5\" width=\"400\" >"; echo "<tr><th>List of Student to be approve</th>"; if(mysql_num_rows($result)) { while($row = mysql_fetch_assoc($result)) { echo "<tr><td>"; echo "<input type='checkbox' name='list[]' value='".$row['Username']."'>".$row['famname'].",".$row['gname']. ",".$row['mname']. ".<br/></td>"; echo "</tr></input>"; } } else { echo "<tr><td align=\"center\"> No Student to be Approve / Reject </td></tr>"; } </form> ?> Then the values will be passed to "valval3.php" and here's my code.. <?php session_start(); if(isset($_SESSION['uname2']) || ($_SESSION['section'])){ $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } if(isset($_POST['approve_x'], $_POST['approve_y'])) { mysql_select_db("dbreg", $con); $uname = $_SESSION['uname2']; $sec = $_SESSION['section']; $sql="SELECT * FROM account where sec='$sec'"; $result=mysql_query($sql); if(mysql_num_rows($result)) { while($row = mysql_fetch_assoc($result)) { $fullname = "" .$row['famname']. ", " .$row['gname']. " " .$row['mname']. "."; } foreach ($_POST['list'] as $checkbox) { $sql="SELECT * FROM account WHERE sec='$sec'"; } $result=mysql_query($sql); if(mysql_num_rows($result)) { while($row = mysql_fetch_assoc($result)) { $username = $row['Username']; $password = $row['Password']; $gname = $row['gname']; $mname = $row['mname']; $famname = $row['famname']; $sec = $row['sec']; $studnum = $row['studnum']; $fullname = "" .$row['famname']. ", " .$row['gname']. " " .$row['mname']. "."; $username = stripslashes($username); $password = stripslashes($password); $username = mysql_real_escape_string($username); $password = mysql_real_escape_string($password); $gname = stripslashes($gname); $famname = stripslashes($famname); $gname = mysql_real_escape_string($gname); $famname = mysql_real_escape_string($famname); $mname = stripslashes($mname); $mname = mysql_real_escape_string($mname); foreach ($_POST['list'] as $checkbox) { mysql_query("INSERT INTO dbaccount (Username, Password, gname, famname, mname, sec, studnum, fullname) VALUES ('$username', '$password', '$gname', '$famname', '$mname', '$sec', '$studnum', '$checkbox')") or die(mysql_error); mysql_query("DELETE FROM account WHERE sec='$sec'") or die(mysql_error); } echo '<script type="text/javascript"> {alert("Approved!");} </script>'; echo '<meta http-equiv="REFRESH" content="0;url=approvereject2.php">'; mysql_close($con); } } else { echo '<meta http-equiv="REFRESH" content="0;url=approvereject2.php">'; } }elseif(isset($_POST['reject_x'], $_POST['reject'])) { mysql_select_db("dbreg", $con); foreach ($_POST['list'] as $checkbox) { $sql="SELECT * FROM account WHERE sec='$sec'"; } $result=mysql_query($sql); if(mysql_num_rows($result)) { foreach ($_POST['list'] as $checkbox) { mysql_query("DELETE FROM account WHERE sec='$sec'") or die(mysql_error); } echo '<script type="text/javascript"> {alert("Rejected!");} </script>'; echo '<meta http-equiv="REFRESH" content="0;url=approvereject2.php">'; } else { echo '<meta http-equiv="REFRESH" content="0;url=approvereject2.php">'; } } else { echo '<meta http-equiv="REFRESH" content="0;url=approvereject2.php">'; } } } else { header("location:failunautho.php");; } ?> So basically.. when there's only one checked values' it will be successful.. but when you checked two values.. The values inserted in the database will be the same(even if you input different values). except for the username.. cause i put a value on the checked box.. Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 13, 2011 Share Posted October 13, 2011 You should format your code using indents to show the logical structure for if/else conditions, loops, etc. Otherwise it becomes unreadable. You should also be using comments. It's kinda hard to understand your code when you simply copy/paste large blocks of code where most of it has nothing to do with the problem. The only checkbox I see in your code is this one: echo "<input type='checkbox' name='list[]' value='".$row['Username']."'>".$row['famname'].",".$row['gname']. ",".$row['mname']. ".<br/></td>"; Then in your second script you use the POST data for 'list' in FOUR different places!!!: foreach ($_POST['list'] as $checkbox) { $sql="SELECT * FROM account WHERE sec='$sec'"; } This is worthless. All this does is redefine the string $sql over and over, but doesn't use it. You then run $sql after the loop completes - so the query is only run for one value - the last one. foreach ($_POST['list'] as $checkbox) { mysql_query("INSERT INTO dbaccount (Username, Password, gname, famname, mname, sec, studnum, fullname) VALUES ('$username', '$password', '$gname', '$famname', '$mname', '$sec', '$studnum', '$checkbox')") or die(mysql_error); mysql_query("DELETE FROM account WHERE sec='$sec'") or die(mysql_error); } You should never run queries in loops!!! foreach ($_POST['list'] as $checkbox) { $sql="SELECT * FROM account WHERE sec='$sec'"; } Same as the first one. All this does is redefine the string $sql over and over, but doesn't use it. You then run $sql after the loop completes - so the query is only run for one value - the last one. foreach ($_POST['list'] as $checkbox) { mysql_query("DELETE FROM account WHERE sec='$sec'") or die(mysql_error); } You should never run queries in loops!!! There is just so much wrong with the code that I don't know where to begin. I'll try to give you some help. Do not set the value of the checkboxes as the username. You should be using the unique ID of the records. I was going to try and pick through your code to find what you need to be doing, but I jsut don't have the time today. But, you are doing way more queries than you need to. For example, you don't need to query one table for values to then insert into another table. You can do an INSERT/UPDATE query that automatically inserts data from another table. I think you need to get out a pencil and paper and work out your logic of what you are trying to achieve and go back and take another run at it. Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 13, 2011 Share Posted October 13, 2011 Ah well, since I already did this work, I'll give you this. Here is your current code which is properly indented to show logical structure. <?php session_start(); if(isset($_SESSION['uname2']) || ($_SESSION['section'])) { $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } if(isset($_POST['approve_x'], $_POST['approve_y'])) { mysql_select_db("dbreg", $con); $uname = $_SESSION['uname2']; $sec = $_SESSION['section']; $sql="SELECT * FROM account where sec='$sec'"; $result=mysql_query($sql); if(mysql_num_rows($result)) { while($row = mysql_fetch_assoc($result)) { $fullname = "" .$row['famname']. ", " .$row['gname']. " " .$row['mname']. "."; } foreach ($_POST['list'] as $checkbox) { $sql="SELECT * FROM account WHERE sec='$sec'"; } $result=mysql_query($sql); if(mysql_num_rows($result)) { while($row = mysql_fetch_assoc($result)) { $username = $row['Username']; $password = $row['Password']; $gname = $row['gname']; $mname = $row['mname']; $famname = $row['famname']; $sec = $row['sec']; $studnum = $row['studnum']; $fullname = "" .$row['famname']. ", " .$row['gname']. " " .$row['mname']. "."; $username = stripslashes($username); $password = stripslashes($password); $username = mysql_real_escape_string($username); $password = mysql_real_escape_string($password); $gname = stripslashes($gname); $famname = stripslashes($famname); $gname = mysql_real_escape_string($gname); $famname = mysql_real_escape_string($famname); $mname = stripslashes($mname); $mname = mysql_real_escape_string($mname); foreach ($_POST['list'] as $checkbox) { mysql_query("INSERT INTO dbaccount (Username, Password, gname, famname, mname, sec, studnum, fullname) VALUES ('$username', '$password', '$gname', '$famname', '$mname', '$sec', '$studnum', '$checkbox')") or die(mysql_error); mysql_query("DELETE FROM account WHERE sec='$sec'") or die(mysql_error); } echo '<script type="text/javascript">{alert("Approved!");}</script>'; echo '<meta http-equiv="REFRESH" content="0;url=approvereject2.php">'; mysql_close($con); } } else { echo '<meta http-equiv="REFRESH" content="0;url=approvereject2.php">'; } } elseif(isset($_POST['reject_x'], $_POST['reject'])) { mysql_select_db("dbreg", $con); foreach ($_POST['list'] as $checkbox) { $sql="SELECT * FROM account WHERE sec='$sec'"; } $result=mysql_query($sql); if(mysql_num_rows($result)) { foreach ($_POST['list'] as $checkbox) { mysql_query("DELETE FROM account WHERE sec='$sec'") or die(mysql_error); } echo '<script type="text/javascript">{alert("Rejected!");}</script>'; echo '<meta http-equiv="REFRESH" content="0;url=approvereject2.php">'; } else { echo '<meta http-equiv="REFRESH" content="0;url=approvereject2.php">'; } } else { echo '<meta http-equiv="REFRESH" content="0;url=approvereject2.php">'; } } } else { header("location:failunautho.php");; } ?> Quote Link to comment Share on other sites More sharing options...
jushiro Posted October 13, 2011 Author Share Posted October 13, 2011 Hey! thx a lot for helping Really appreciate it a lot.. I found what's wrong with my code.. I edited it and its working' xD.. srry for that.. but i was still studying php.. so i dont know yet the best method of what to use. so i just write what i know. but anyway. its fixed! thx for the help Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.