dannyb785 Posted March 5, 2012 Share Posted March 5, 2012 So I've been scanning forms with checkboxes the same way for years and I can't help but think there's a better way. Sometimes when testing, it get caught in infinite loops the way I do it because basically I'll have it like so: formpage.php <form action=processform.php> a1 <input type=hidden name=id value='1'> <input type=checkbox name=a1> <br> a2 <input type=hidden name=id value='2'> <input type=checkbox name=a2> // etc etc for all checkboxes and then, <input type=hidden name=tracker value='-1000'> <input type=submit value='Submit'> </form> processform.php while(current($_POST) != '-1000') { // do stuff with current($_POST) to check the id and the value of the $_POST after it and then call next($_POST) to go to the next line } The reason I do this is because if a user clicks the a1 box, there is a value in the $_POST['a1'], but all boxes that are not checked do not pass through a variable. There must be a better way to do this Quote Link to comment Share on other sites More sharing options...
scootstah Posted March 5, 2012 Share Posted March 5, 2012 Huh? I don't really understand what you are asking, but you can check if checkboxes are checked in this way: <?php if (!empty($_POST)) { $check = isset($_POST['check']) ? (bool) $_POST['check'] : false; var_dump($check); } ?> <form action="" method="post"> <input type="checkbox" name="check" value="1"> <input type="submit" name="submit" value="submit" /> </form> $check will either be "true" if it is checked, or "false" if it isn't. Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted March 5, 2012 Author Share Posted March 5, 2012 Did you not understand when I said scanning numerous checkboxes, and then gave an example of 2 checkboxes next to each other? When you have a form with multiple checkboxes next to each other, the only way(it seems) to know if the box was checked is to see if the $_POST variable for that box is set to the input's value. So for <input type=checkbox name=a1 value='bob'>, you'd need to check if($_POST['a1'] == "bob"). But if you don't know how many checkboxes you're gonna have, what do you do? The thing is, most of my checkboxes are dynamically output in php(by grabbing users from a database and making a checkbox for each of them) so I can't make a loop that scans all possible checkboxes, plus just the value of the checkbox alone isn't enough to tell me which user that checkbox corresponds to. Quote Link to comment Share on other sites More sharing options...
scootstah Posted March 5, 2012 Share Posted March 5, 2012 When you have a form with multiple checkboxes next to each other, the only way(it seems) to know if the box was checked is to see if the $_POST variable for that box is set to the input's value. So for <input type=checkbox name=a1 value='bob'>, you'd need to check if($_POST['a1'] == "bob"). And if "a1" wasn't set you would get an undefined index error with that code. If the checkbox isn't checked and you submit the form, you won't get any $_POST data at all about that checkbox. So simply checking if the $_POST value exists is enough to know if the checkbox was checked or not. You can do that with if (isset($_POST['a1'])) ... The thing is, most of my checkboxes are dynamically output in php(by grabbing users from a database and making a checkbox for each of them) so I can't make a loop that scans all possible checkboxes, plus just the value of the checkbox alone isn't enough to tell me which user that checkbox corresponds to. You can turn the checkboxes into an array, and then you can figure out how many of them there are. For example... <?php if (!empty($_POST)) { $users = isset($_POST['user']) ? $_POST['user'] : array(); foreach(array_keys($users) as $key) { echo 'User "' . $key . '" is set!<br />'; } } ?> <form action="" method="post"> <input type="checkbox" name="user[a1]" value="1"> <input type="checkbox" name="user[a2]" value="1"> <input type="checkbox" name="user[a3]" value="1"> <input type="checkbox" name="user[a4]" value="1"> <input type="submit" name="submit" value="submit" /> </form> 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.