Jump to content

[SOLVED] checkbox does not send 'false' value


sloth456

Recommended Posts

I've tried googling it and I've looked around on the forum but I have another annoying checkbox problem.

 

Say I have 3 checkboxes.  They directly effect the data that is pulled from a database and placed on the same page.  (the form and data are on the same page).

 

By default all 3 boxes are checked.  On the frontend I've just added the "checked=yes" attribute to each checkbox so they appear checked.

 

On the backend I've just coded the following

 


$value1="checked";
$value2="checked";
$value3="checked";

 

Trouble is when I uncheck the boxes the code doesn't process it properly.  Since the checkbox's don't actually send a 'false' value, they just send nothing at all, so I use isset() to determine whether they are checked or not, like so.

 


$value1="checked";
$value2="checked";
$value3="checked";

if(!isset($_GET['checkbox1'])
{
$value="unchecked";
}

if(!isset($_GET['checkbox2'])
{
$value2="unchecked";
}

if(!isset($_GET['checkbox3'])
{
$value3="unchecked";
}

 

This doesn't work because when the page is first loaded the GET variables are not set, so the script puts all my $values to "unchecked" when I want them to remain "checked".

 

I know I've described it in a bit of a confusing way.  If you need any clarification just ask and I'll do my best.

Link to comment
Share on other sites

I don't think you understood what I'm trying to do.

 

Lets reduce to just one checkbox to make things simple.

 

I want $value to be '1' when the checkbox is checked. '0' when the checkbox is unchecked. And  '1' when the page first loads.

 

Perhaps I'm being stupid, but I can't find a simple way to do that.

Link to comment
Share on other sites

well that's not even what you were attempting to do above, you were assigning either "" or "checked", but if you want to do 1 or 0, do it like this i guess:

 

$value1 = 0;

if(isset($_GET['checkbox1'])
{
$value1=1;
}

// within the form:
<input type="checkbox" name="checkbox1" value="whatever" <?php ($value == 1 ? echo "checked=\"checked\"") ?> />

Link to comment
Share on other sites

Smerny, I know I was assigning checked and unchecked before, I changed it to make it easier to understand.

 

Thanks for all your helpso far, it's really appreciated, however in the code you've given me when the page first loads $value1 will be set to 0.  It needs to be 1.

Link to comment
Share on other sites

it doesn't make any difference to the end result, and to me it makes more sense to have it set to off and turn it on... but if you want to do it the other way, just invert everything in that part...

 

from

$value1 = 0;

if(isset($_GET['checkbox1'])
{
$value1=1;
}

to

$value1 = 1;

if(!isset($_GET['checkbox1'])
{
$value1=0;
}

Link to comment
Share on other sites

Now when the page first loads $value1 will still be set to 0. 

 

When the page first loads the $_GET variable will not be set.  Thus the !isset() clause returns true and $value becomes 0.

 

I've tried all these things myself, this is precisely the problem.

 

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.