EHTISHAM Posted November 19, 2015 Share Posted November 19, 2015 Limit selection of check box My code looks like... foreach($res as $res) echo '<div class="ediv"><input type="checkbox" class="echeck" name="pr[]" value="'.trim($res['product']).'"/>'.trim($res['product']).'</div>'; How to set limit of selection of dynamically created checkboxes...?? Quote Link to comment https://forums.phpfreaks.com/topic/299521-limit-selection-of-check-box/ Share on other sites More sharing options...
EHTISHAM Posted November 19, 2015 Author Share Posted November 19, 2015 dynamically created code looks like this in browser... <div class="ediv"> <input type="checkbox" value="Water" name="pr[]" class="echeck">Water </div> i tried too many logics.. but all are not working.. Anyone know how to do this? Quote Link to comment https://forums.phpfreaks.com/topic/299521-limit-selection-of-check-box/#findComment-1526735 Share on other sites More sharing options...
maxxd Posted November 19, 2015 Share Posted November 19, 2015 Limit selection in what way? A little more explanation, please. Quote Link to comment https://forums.phpfreaks.com/topic/299521-limit-selection-of-check-box/#findComment-1526742 Share on other sites More sharing options...
ginerjm Posted November 19, 2015 Share Posted November 19, 2015 I have to wonder if foreach ($res as $res) will work. You are supplanting the $res variable with a new value so you lose the rest of the array! Try foreach ($res as $item) instead and then manipulate $item Quote Link to comment https://forums.phpfreaks.com/topic/299521-limit-selection-of-check-box/#findComment-1526785 Share on other sites More sharing options...
EHTISHAM Posted November 20, 2015 Author Share Posted November 20, 2015 Limit selection in what way? A little more explanation, please. Limit selection in what way? A little more explanation, please. ok.. the task is that.. a user cannot select more than 3, 4 checkbox.. if user exceeds the limit than he get a alert message... Quote Link to comment https://forums.phpfreaks.com/topic/299521-limit-selection-of-check-box/#findComment-1526809 Share on other sites More sharing options...
EHTISHAM Posted November 20, 2015 Author Share Posted November 20, 2015 I have to wonder if foreach ($res as $res) will work. You are supplanting the $res variable with a new value so you lose the rest of the array! Try foreach ($res as $item) instead and then manipulate $item it works.. thanks for sharing knowledge.. i'll change $res to $item.. How to set limit of selection of dynamically created checkboxes...?? Quote Link to comment https://forums.phpfreaks.com/topic/299521-limit-selection-of-check-box/#findComment-1526811 Share on other sites More sharing options...
Jacques1 Posted November 20, 2015 Share Posted November 20, 2015 Unchecked boxes aren't submitted at all, so you just have to count the elements of $_POST['pr'] (if you're using the POST method): if (isset($_POST['pr']) && count($_POST['pr']) > 4) { echo 'You can select at most 4 options.'; } Quote Link to comment https://forums.phpfreaks.com/topic/299521-limit-selection-of-check-box/#findComment-1526812 Share on other sites More sharing options...
maxxd Posted November 20, 2015 Share Posted November 20, 2015 If you want to do it before the user submits the form, use JavaScript. When a select box is checked, increment a variable. When the user tries to select the fifth checkbox, pop up an alert box and uncheck the last selection. Of course, you'll have to decrement the variable when a select box is deselected. Quote Link to comment https://forums.phpfreaks.com/topic/299521-limit-selection-of-check-box/#findComment-1526817 Share on other sites More sharing options...
Jacques1 Posted November 20, 2015 Share Posted November 20, 2015 Hm, that sounds pretty annoying, especially when it's a dialog that forces the user to click some silly “OK” button for he can do anything else. If at all, I'd display a warning next to the checkboxes so that the user can decide for himself when to fix the problem. Quote Link to comment https://forums.phpfreaks.com/topic/299521-limit-selection-of-check-box/#findComment-1526827 Share on other sites More sharing options...
EHTISHAM Posted November 22, 2015 Author Share Posted November 22, 2015 Unchecked boxes aren't submitted at all, so you just have to count the elements of $_POST['pr'] (if you're using the POST method): if (isset($_POST['pr']) && count($_POST['pr']) > 4) { echo 'You can select at most 4 options.'; } I have to do it before submit the form.. any idea? Quote Link to comment https://forums.phpfreaks.com/topic/299521-limit-selection-of-check-box/#findComment-1526929 Share on other sites More sharing options...
EHTISHAM Posted November 22, 2015 Author Share Posted November 22, 2015 If you want to do it before the user submits the form, use JavaScript. When a select box is checked, increment a variable. When the user tries to select the fifth checkbox, pop up an alert box and uncheck the last selection. Of course, you'll have to decrement the variable when a select box is deselected. Will you please share the code of java script function to do this.. Quote Link to comment https://forums.phpfreaks.com/topic/299521-limit-selection-of-check-box/#findComment-1526930 Share on other sites More sharing options...
Jacques1 Posted November 22, 2015 Share Posted November 22, 2015 Since you want a JavaScript solution, I'll move this thread to the JavaScript forum. In any case, you have to do the check server-side as well, because otherwise people will just disable your JavaScript code and select as many checkboxes as they want. Quote Link to comment https://forums.phpfreaks.com/topic/299521-limit-selection-of-check-box/#findComment-1526931 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.