Jump to content

Little help needed with a checkbox...


kelemvor

Recommended Posts

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!

 

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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 by kelemvor
Link to comment
Share on other sites

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. :)

Link to comment
Share on other sites

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.

 

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

$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 by Q695
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.