vinsux Posted March 11, 2013 Share Posted March 11, 2013 //some php sql codes echo "<form action='survey.php' method='POST'>"; echo "<td class = name> <input type='checkbox' name='vote[]' value='$sql_row[name]' id='$sql_row[id]'>".$name."</td>"; echo "</tr>"; } } echo "<tr><td class = btn><button type='submit' name='btnSurvey'>Vote</button></td> </tr></form>"; echo "<form action='viewsurvey.php' method='POST'>"; echo "<tr><td class = btn><button type='submit' name='btnResult'>Results</button></td> </tr></form>"; echo "</table>"; //echo " <tr><td><button type='submit' name='btnSurvey'>Vote</button></td> </tr></form>"; } mysql_close(); include('connect.php'); $result = mysql_query("SELECT * from candidates"); //adding only one candidate if(!empty($_POST['vote'])) { foreach($_POST['vote'] as $check) { // echo $check; mysql_query("UPDATE candidates SET votes = votes + 1 WHERE name = '$check'"); header("Location: viewsurvey.php"); exit(); } } // some codes i found a bug on my work, my work is about survey, users will choose from 20+ candidates and they need to select only 12.. i used check box for user to select more than one candidate to vote, i want to set it until 12 but i can only add one vote on the candidate even i checked (or vote) more than one. help please.. Quote Link to comment https://forums.phpfreaks.com/topic/275520-helpabout-check-box-adding-in-sql/ Share on other sites More sharing options...
Psycho Posted March 11, 2013 Share Posted March 11, 2013 So, you have multiple checkbox options for the user to choose from (at least 20) but you want to limit the user to only selecting up to 12, correct? The best solution for this, in my opinion is to 1) Implement JavaScript to disable the checkboxes (or provide some type of user response) once 12 options are selected. But, you must also 2) implement code on the back-end page that receives the form submission to verify that there were no more than 12 options selected. Quote Link to comment https://forums.phpfreaks.com/topic/275520-helpabout-check-box-adding-in-sql/#findComment-1418011 Share on other sites More sharing options...
Psycho Posted March 11, 2013 Share Posted March 11, 2013 (edited) Here is an example script that has both the JavaScript and the PHP code to implement the client-side logic and the server-side verification. To test the server-side logic you would need to to one of three things: 1. Turn off javascript in your browser so you can select more than 12 options 2. Remove the onclick even in the checkboxes 3. In the limitCheckboxes() javascript function put a "return;" on the very first lime. This will bypass the Javascript function and allow you to select more than 12 options for the purpose of testing. I put such a line into the function (which is commented out) and you can uncomment it to test. <?php error_reporting(E_ALL); $maxVoteCount = 12; //If form was posted verify number of checked items $result = ""; if($_SERVER['REQUEST_METHOD'] == "POST") { if(isset($_POST['vote']) && count($_POST['vote']) > $maxVoteCount) { $result = "You checked too many items. Bad Job."; } else { $result = "You did not check too many options. Good Job."; } } //Dynamically create input fields $inputFields = ""; for($i = 1; $i <=20; $i++) { $inputFields .= "<input type='checkbox' name='vote[]' value='{$i}' id='{$i}' onclick='limitCheckboxes(this.name, {$maxVoteCount})'> Option {$i}<br>\n"; } ?> <html> <head> <script type="text/javascript"> function limitCheckboxes(groupName, maxCount) { //Uncomment next line to test PHP code for verifying max count //return; var checkboxGroup = document.getElementsByName(groupName); var checkedCount = 0; for(i=0; i<checkboxGroup.length; i++) { checkedCount += checkboxGroup[i].checked; } //Set disable state based upon number checked var disabledState = (checkedCount >= maxCount); for(i=0; i<checkboxGroup.length; i++) { //disable/enable the unchecked boxes if(!checkboxGroup[i].checked) { checkboxGroup[i].disabled = disabledState; } } //Enable/disable submit button document.getElementById('submit').disabled = disabledState; return false; } </script> </head> <body> <?php echo $result; ?> <br><br> Select up to 12 options: <br><br> <form action="" method="post"> <?php echo $inputFields; ?> <br> <button type="submit" id="submit">Submit</button> </form> </body> </html> Edited March 11, 2013 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/275520-helpabout-check-box-adding-in-sql/#findComment-1418023 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.