kelemvor Posted May 23, 2013 Share Posted May 23, 2013 Howdy, I'm a complete newb at PHP but I have some code that I'm trying to modify and am not having any luck. It's basically a form with some options and I added a checkbox that I just need to check if it's checked or not. Some of the checks it currently does are setup in the format of: if (!empty($data['date_filter'])) { if (isset($data['start']) || isset($data['limit'])) { I have a checkbox configured as: <input type"checkbox" name="filter_cust" value=""></td> I tried an if statement setup as: if (isset($data['filter_cust'])) { However it doesn't seem to see that the checkbox is checked and do what I need it to do. Is there a different formatting I should be using to check if a checkbox is checked? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/278329-little-help-needed-with-a-checkbox/ Share on other sites More sharing options...
DavidAM Posted May 23, 2013 Share Posted May 23, 2013 Checkboxes are only submitted if they are checked. So your logic using isset() should work. The only issue I see is that there is no value provided in the HTML, so there is nothing to submit if it is checked. You might try: <input type"checkbox" name="filter_cust" value="1">This assumes that somewhere you have copied the data from $_POST to $data. You might check the code that does that copy to see if it is only copying specific keys. Quote Link to comment https://forums.phpfreaks.com/topic/278329-little-help-needed-with-a-checkbox/#findComment-1431889 Share on other sites More sharing options...
kelemvor Posted May 23, 2013 Author Share Posted May 23, 2013 (edited) Ah, I did find the data array and added that in but ran into some other trouble. Looks like thsi is going to get complicated pretty quick. heh. I think I might just do it manually as it involves 3 or 4 files talking back and forth doing things I don't understand. Thanks for the help. If anyone is really bored and wants to decipher this whole thing for me, let me know and I'll provide the files. But it looks to be a bit more involved as it's creating an sql query and then passing the results somehwere to then create the ouput etc. I think I'll just run the query manually since I know that works. Edited May 23, 2013 by kelemvor Quote Link to comment https://forums.phpfreaks.com/topic/278329-little-help-needed-with-a-checkbox/#findComment-1431898 Share on other sites More sharing options...
kelemvor Posted May 23, 2013 Author Share Posted May 23, 2013 Hmm, guess I can't edit my psot after a certain time? Anyway, I'm back with more info. I fumbled my wya through this and actualyl got everything working except the darn chekbox. Here's my IF statement (it's part of creating an SQL query) if (isset($data['filter_cust_group'])) { $sql .= " GROUP BY x"; } else { $sql .= " GROUP BY y"; } Basically if the box is checked, Group By one thing and if it's not checked, Group By something else. The part of the IF command that is running is the first section. So whether or not the box is checked, it still runs the first Group By statement. The checkbox is: (I tried with and without the "1" in there) <td><?php echo $entry_cust_group; ?> <input type="checkbox" name="filter_cust_group" value="1"></td> In the $data array, there's a line that says: $data = array( ... 'filter_cust_group' => $filter_cust_group does that maybe need to be changed somehow? I don't see an actual $_post command anywhere for the item I added or any of the other items but they all work fine. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/278329-little-help-needed-with-a-checkbox/#findComment-1431944 Share on other sites More sharing options...
DavidAM Posted May 23, 2013 Share Posted May 23, 2013 In the $data array, there's a line that says: $data = array( ... 'filter_cust_group' => $filter_cust_groupdoes that maybe need to be changed somehow? I don't see an actual $_post command anywhere for the item I added or any of the other items but they all work fine. With so little code provided, I can only guess. But this snippet will unconditionally set the $data['filter_cust_group'] element, so isset will always be true. You should set some value in the HTML (not zero and not an empty string), then use empty instead of isset. if (empty($data['filter_cust_group'])) { // The box is NOT checked } else { // The box IS checked }Also, it is important to note, this snippet and the fact that you can't find $_POST in the code, would indicate that your code relies on register_globals to be on. This "feature" of PHP has been deprecated and has been removed in the most recent releases. Your code will break if your PHP engine is updated in the future. You need to think about revising your code to NOT depend on this "feature". P.S. Yes, there is a time limit on editing posts on this forum. Quote Link to comment https://forums.phpfreaks.com/topic/278329-little-help-needed-with-a-checkbox/#findComment-1431950 Share on other sites More sharing options...
Q695 Posted May 24, 2013 Share Posted May 24, 2013 is the check box being passed as a $_GET, or $_POST? look for it with a print_r() you need a checkbox value, or it will look at it automatically NULL. Quote Link to comment https://forums.phpfreaks.com/topic/278329-little-help-needed-with-a-checkbox/#findComment-1432012 Share on other sites More sharing options...
kelemvor Posted May 24, 2013 Author Share Posted May 24, 2013 So here's the whole $data section after I modified it to do the checkbox 2 different ways: $data = array( 'filter_date_start' => $filter_date_start, 'filter_date_end' => $filter_date_end, 'filter_order_status_id' => $filter_order_status_id, 'start' => ($page - 1) * $this->config->get('config_admin_limit'), 'limit' => $this->config->get('config_admin_limit'), 'filter_cust_group' => $this->config->get('filter_cust_group') // 'filter_cust_group' => $filter_cust_group ); If I have the last line commented out, it does the Else part of the isset statement regardless of if the box is checked or not. If I have the 2nd to last line commented out, it does the first part of the it statement regardless. Does that help? Quote Link to comment https://forums.phpfreaks.com/topic/278329-little-help-needed-with-a-checkbox/#findComment-1432066 Share on other sites More sharing options...
DavidAM Posted May 24, 2013 Share Posted May 24, 2013 Does that help?Not really. Is this a WordPress site, or is some other framework involved here? Did you change the form field (the checkbox) to include value="1"? Did you change the IF test to use empty instead of isset? Is the FORM tag using POST or GET as the method? For debugging purposes, add the following lines immediately after the $data = array( ...); line that you showed above: printf('data: <PRE>%s</PRE>', htmlspecialchars(print_r($data, true))); printf('POST: <PRE>%s</PRE>', htmlspecialchars(print_r($_POST, true))); printf('GET: <PRE>%s</PRE>', htmlspecialchars(print_r($_GET, true))); die();Then submit the form with the checkbox checked. Copy and Paste the results (in code tags) here. Also, post the code in that script from the beginning up to and including the $data = array(... statement. Quote Link to comment https://forums.phpfreaks.com/topic/278329-little-help-needed-with-a-checkbox/#findComment-1432074 Share on other sites More sharing options...
Q695 Posted May 24, 2013 Share Posted May 24, 2013 (edited) $data = array( 'filter_date_start' => $filter_date_start, 'filter_date_end' => $filter_date_end, 'filter_order_status_id' => $filter_order_status_id, 'start' => ($page - 1) * $this->config->get('config_admin_limit'), 'limit' => $this->config->get('config_admin_limit'), 'filter_cust_group' => $this->config->get('filter_cust_group') // 'filter_cust_group' => $filter_cust_group ); Why are you using an array to hold the data instead of just using things like "$filter_cust_group"? Also you have 2 "filter_cust_group"s. Edited May 24, 2013 by Q695 Quote Link to comment https://forums.phpfreaks.com/topic/278329-little-help-needed-with-a-checkbox/#findComment-1432077 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.