Jump to content

I hate checkboxes; please help me process them.


bluegray

Recommended Posts

I'm trying to build a function that processes the (x)html checkbox. 

 

I'm stuck at trying to extract the keys from the array that makes it through the form.

 

Here is the full function so far:

 

$boxdata = isset($data)?$data:null ;
var_dump($boxdata) ;

foreach ($boxdata as $bd) {
	$key = key ($boxdata) ; 
	$keys[] = $key ;
	next ($boxdata) ;
}
var_dump ($keys);
$fields = implode (', ', $keys) ;
//return $fields ;
echo "<p>$fields</p>" ;

 

Now, when I var_dump  $boxdata, I get the full 4 value array.

 

When I var_dump $keys I get a 4 value array that's been shifted.  The first value from $boxdata is missing, and the 4th value is suddenly null.  So by the time I echo "$fields" I'm missing the first element in the $boxdata array.

 

The most irritating part is this function worked when dealing with "text" type inputs, but the "checkbox" just keeps trying to kick me down.

 

What...the heck?

 

Thanks for your help Php Freaks.

Link to comment
Share on other sites

Well, makes sense to me. You're making the code more complex than it needs to be. But here is the specific problem:

	foreach ($boxdata as $bd) {
	$key = key ($boxdata) ; 

 

key() "...returns the index element of the current array position"

 

So, on the first iteration of the foreach() loop you get the first value of the array assigned to $bd. But at that same instance, the array pointer is shifted to the next element. So, the $key value you get is for the second value in the array, not the first. You don't need to go to all that trouble. That loop does nothing more than get the keys, which you can easily do with array_keys();

 

$boxdata = isset($data) ? $data : array();

var_dump($boxdata);

$keys = array_keys($boxdata);

var_dump ($keys);

$fields = implode (', ', $keys) ;

//return $fields ;
echo "<p>$fields</p>" ;

 

By the way, you should be using the VALUES of the checkboxes not the names (which is what it seems you are doing). Just give all the checkboxes the same name such as "fields[]". Then you can reference all of the checked values using the post value $_POST['fields'] without all this extra effort.

$fields = implode (', ', $_POST['fields']) ;

Link to comment
Share on other sites

mjdamato

 

I used the array_keys function which worked well, but what confused me is how that same function I used worked exactly as intended with a text type input.  With virtually the same format, barring the word "checkbox" in the type attribute, it changed the results.

 

Also, I mainly used the names of the element as it differs from the value because of the separation between fields and values in the query.  But furthermore, and perhaps more importantly, I'm mostly just doing function testing with largely arbitrary values so I can get a feel for how things work. 

 

I want to make a function that takes a variety of forms and processes them without having to manually write the database queries.

Link to comment
Share on other sites

We

... what confused me is how that same function I used worked exactly as intended with a text type input.  With virtually the same format, barring the word "checkbox" in the type attribute, it changed the results.

 

Well, I don't know how the $data variable is created/set. but, if there is any logic to match it up against a known set of "possible" values, that might be the problem.

 

If you have 10 checkboxes and only 8 are checked, then the post data only contains 8 fields. The two unchecked fields are not part of the post data. That might explain your results.

Link to comment
Share on other sites

A new problem came up.  Apparently, when I don't check any of the boxes, I get this funky message:

 

NULL
Notice: Undefined variable: keys in C:\wamp\www\test\functions.inc.php on line 38
NULL
Notice: Undefined variable: keys in C:\wamp\www\test\functions.inc.php on line 39

Warning: implode() [function.implode]: Invalid arguments passed in C:\wamp\www\test\functions.inc.php on line 39

 

What should I do to counteract it?  Because if I check at least one, everything moves as expected.

Link to comment
Share on other sites

That is just a notice which would not be displayed in a production environment since you should not have error reporting set to show such messages. But, it's good to resolve those types of issues nonetheless.

 

Basically, the error is due to the fact that you are trying to reference a variable that does not exist. Since none of the checkboxes are checked. So, you simply need to utilize an isset() in an if statement to only run the code for processign the checkboxes if, well, it's set.

 

if(isset($_POST['keys']))
{

    //Code to process the checkbox data

}

 

Or, instead of having to break out that functionality entirely, you can just add a single line to ensure the value is set if no checkboxes are checked. just set it to an empty array.

 

if(!isset($_POST['keys']))
{
    $_POST['keys'] = array();
}

//Code to process checkboxes continues here

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.