julieho Posted March 22, 2009 Share Posted March 22, 2009 Hi! I have a award show where people can nominate their favourites, and then vote on their favourites. I have a script (code below) that handles the outputting of polls and the vote counting. For every poll it prints out the category name and every nominee in that category from the database. For an example: Nomination Category 1 Nominee 1 Nominee 2 Nomination Category 2 Nominee 1b Nominee 2b I want to be able to limit the number of checkboxes a user can select per category, f. ex only 2 checkboxes per poll category. So first question is, how can I do that with a javascript that'll notify the user when he/she has selected one to many (that'll work with my script below), and also check it in the PHP script in case someone has javascript turned off. Also I want to notify the user of where the error occurred. Say if they have javascript turned off, check one to many checkboxes per category, and hits submit. I then want to display the polls again, with their choices already checked and then an error message below the poll where the error occurred. The code below is probably a bit messy, so if you have any optimization tricks, let me know! Thanks in advance! Code roughly explained: At first it checks if the voting is opened (gets the value from a config file), then if it is open, it checks for a cookie. If there is a cookie, it kills the script. If there is not a cookie, then it prints out the polls and poll answers getting the data from the database. If the user clicks on the submit button, the script loops through the $_POST getting the poll category and then updates the votes for the checked answers in that category. <?php if (VOTEOPEN != FALSE) { // check if a cookie exists for this question // deny access if it does if (isset($_COOKIE) && !empty($_COOKIE)) { if ($_COOKIE['nslastpoll']) { die('<h1>Polls</h1><p>You have already voted in this poll.</p>'); } } $errors = array(); $hideform = FALSE; // generate and execute query $result = DBConnection::getInstance()->query("SELECT qid, qtitle FROM $table_poll_questions "); // ORDER BY qdate DESC LIMIT 0, 1"); $numPolls = mysqli_num_rows($result); if (isset($_POST['submitPoll'])) { if (count($_POST['answer']) < $numPolls) { echo "Please select at least one answer from every category."; } else { if (!empty($_POST['answer'])) { foreach ($_POST['answer'] as $s) { //if (count($_POST['answer']) >= 2) { //count($_POST['checkbox']); //$errors['question'] = "You can only choose 2 in every category."; //} else { $result3 = DBConnection::getInstance()->query("UPDATE $table_poll_answers SET acount = acount + 1 WHERE aid = '$s'"); //} } } // set cookie setCookie('nslastpoll', time() + 2592000); } if ($result3) { echo "<div class=\"success\">Your vote was successfully registered!</div>\n\n"; $hideform = TRUE; } else { echo 'ERROR: Data not correctly submitted'; } } if (!$hideform) { echo '<h1>Polls</h1>'; $empty = checkEmpty($result, "Sorry, no polls available yet!"); if ($empty != 0) { echo "\t<p>Below you can vote on the selected polls. You can only select two from each category</p>\n"; // if records are present echo "\n\n\t<form method=\"post\" id=\"form\" action=\"".getCurrentUrl()."\">\n\n"; while ($row = mysqli_fetch_assoc($result)) { echo "\t<h3>".$row['qtitle'] . "</h3>\n"; // Print out the poll question title echo "\t<input type=\"hidden\" name=\"question[]$row[qid]\" value=\"".$row['qid']."\" />\n"; // get possible answers using question ID $result2 = DBConnection::getInstance()->query("SELECT aid, atitle FROM $table_poll_answers WHERE qid = $row[qid]"); // print answer list as radiobuttons/checkboxes while ($row2 = mysqli_fetch_assoc($result2)) { echo "\t<input type=\"checkbox\" name=\"answer[]$row[qid]\" value=\"".$row2['aid']."\" "; /* if (isset($_POST['answer[]'])) { echo "checked=\"checked\""; } */ echo " />".$row2['atitle'] ."<br />\n"; // Print out the answer title } error_msg('question'); // If there is an error, print out the error message her } echo "\t<br /><input type=\"submit\" name=\"submitPoll\" value = \"Vote!\" />\n"; echo '</form>'; } } else { // If hide form echo "<h1>Polls</h1>"; echo "<p>Thank you for your votes </p>"; } } else { echo "<h1>Polls</h1>"; echo "<p>Sorry, the voting period is not yet open! </p>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/150557-limit-number-of-checkboxes-remember-those-that-were-checked-and-print-error-msg/ Share on other sites More sharing options...
Maq Posted March 22, 2009 Share Posted March 22, 2009 I'm not going through your code but here are some pseudo solutions. I want to be able to limit the number of checkboxes a user can select per category, f. ex only 2 checkboxes per poll category. So first question is, how can I do that with a javascript that'll notify the user when he/she has selected one to many (that'll work with my script below), and also check it in the PHP script in case someone has javascript turned off. When the page is submitted put the selected checkboxes in a array and just do count($array) > 'max' to ensure they don't select too many. Also I want to notify the user of where the error occurred. Say if they have javascript turned off, check one to many checkboxes per category, and hits submit. I then want to display the polls again, with their choices already checked and then an error message below the poll where the error occurred. You can use arrays. When you do your checks with PHP just keep adding to the array. i.e: if(count($array) > 5) { $errors[] = "Too many selected"; } Then at the end check to see if you have errors. If you do, display them, if not do whatever: if($errors) { foreach($errors as $error) { echo $error . " "; } else { echo "Successful"; } Hope this helps! Quote Link to comment https://forums.phpfreaks.com/topic/150557-limit-number-of-checkboxes-remember-those-that-were-checked-and-print-error-msg/#findComment-791096 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.