bluegray Posted June 11, 2010 Share Posted June 11, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/204515-i-hate-checkboxes-please-help-me-process-them/ Share on other sites More sharing options...
Psycho Posted June 11, 2010 Share Posted June 11, 2010 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']) ; Quote Link to comment https://forums.phpfreaks.com/topic/204515-i-hate-checkboxes-please-help-me-process-them/#findComment-1070902 Share on other sites More sharing options...
bluegray Posted June 11, 2010 Author Share Posted June 11, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/204515-i-hate-checkboxes-please-help-me-process-them/#findComment-1070939 Share on other sites More sharing options...
Psycho Posted June 11, 2010 Share Posted June 11, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/204515-i-hate-checkboxes-please-help-me-process-them/#findComment-1070947 Share on other sites More sharing options...
bluegray Posted June 15, 2010 Author Share Posted June 15, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/204515-i-hate-checkboxes-please-help-me-process-them/#findComment-1072561 Share on other sites More sharing options...
Psycho Posted June 15, 2010 Share Posted June 15, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/204515-i-hate-checkboxes-please-help-me-process-them/#findComment-1072567 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.