Jump to content

Scanning a form with numerous checkboxes


dannyb785

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.