Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Unchecking a Checkbox
Old 04-19-2010, 08:38 PM Unchecking a Checkbox
portkey's Avatar
Skilled Talker

Posts: 55
Name: Erica Dion
Trades: 0
I have a form with a checkbox for every day in the week. Using PHP, I take some information from a mysql database and if the value is checked there, I declare the checkbox checked. i.e.

PHP Code:
$W 1
Code:
<input type="checkbox" name="W"<? if($W==1) echo " checked"; ?> /> Wednesday
Since $w = 1 the above box will output like this:
Code:
<input type="checkbox" name="W" checked" /> Wednesday
I'll then uncheck the box, and resubmit the form. However, the below line will still print out 1 instead of 0.
PHP Code:
$W = ((!empty($_POST['W'])) && (strtolower($_POST['W']) != "off")) ? 0
Is this because of how it's checked? I've been staring at it for quite some time now and haven't been able to figure it out.
__________________
Hysterical Web Design

Please login or register to view this content. Registration is FREE

Fair priced and reliable web design services that complement your website needs
portkey is offline
Reply With Quote
View Public Profile Visit portkey's homepage!
 
 
Register now for full access!
Old 04-19-2010, 10:13 PM Re: Unchecking a Checkbox
Extreme Talker

Posts: 246
Trades: 3
Your form method is "post", correct?
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
CouponGuy is offline
Reply With Quote
View Public Profile
 
Old 04-19-2010, 10:21 PM Re: Unchecking a Checkbox
MattGoucher's Avatar
Skilled Talker

Posts: 64
Name: Matt
Location: California
Trades: 0
Here is a really basic script that will do what you need. There are certainly more dynamic ways of going about this, but this is in my opinion very simple for beginners.

PHP Code:
<?php
    
// Incoming Values From The Database
    
$MON '1';
    
$TUE '0';
    
$WEN '0';
    
$THU '0';
    
$FRI '0';
    
$SAT '0';
    
$SUN '0';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <title>Checkbox Training Area</title>
    <meta name="DC.title" content="" />
    <meta name="DC.subject" content="" />
    <meta name="DC.description" content="" />
    <meta name="DC.format" content="text/html" />
    <meta name="DC.publisher" content="" />
    <meta name="DC.language" content="en" />
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
</head>
<body>
    <form method='post' action='index.php'>
        <input type='checkbox' name='MON' <?php if($MON) echo "checked"?> /> 
        <input type='checkbox' name='TUE' <?php if($TUE) echo "checked"?> /> 
        <input type='checkbox' name='WEN' <?php if($WEN) echo "checked"?> /> 
        <input type='checkbox' name='THU' <?php if($THU) echo "checked"?> /> 
        <input type='checkbox' name='FRI' <?php if($FRI) echo "checked"?> /> 
        <input type='checkbox' name='SAT' <?php if($SAT) echo "checked"?> /> 
        <input type='checkbox' name='SUN' <?php if($SUN) echo "checked"?> /> 
    </form>
</body>
</html>
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Invalid Code On A New Website Is Like Having A New Car With A Broken Windshield
MattGoucher is offline
Reply With Quote
View Public Profile Visit MattGoucher's homepage!
 
Old 04-19-2010, 10:29 PM Re: Unchecking a Checkbox
Extreme Talker

Posts: 246
Trades: 3
A few more things:

Before I keep speculating, it would be useful to me (and hopefully I'm not alone) if you posted more/all of your code.

The following shouldn't print out anything, but I think I know what you mean:
Quote:
I'll then uncheck the box, and resubmit the form. However, the below line will still print out 1 instead of 0.
Code:
$W = ((!empty($_POST['W'])) && (strtolower($_POST['W']) != "off")) ? 1 : 0;
Finally, this portion of logic:
Code:
(strtolower($_POST['W']) != "off")
will always result in TRUE, so you might as well leave it out, and just have:
Code:
$W = (!empty($_POST['W'])) ? 1 : 0;
A checkbox with name="W" will either be "on" or nothing, but I don't believe it will ever be equal to "off". It's one of the annoying things about validating forms.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
CouponGuy is offline
Reply With Quote
View Public Profile
 
Old 04-19-2010, 10:36 PM Re: Unchecking a Checkbox
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Quote:
Originally Posted by CouponGuy View Post
A checkbox with name="W" will either be "on" or nothing, but I don't believe it will ever be equal to "off". It's one of the annoying things about validating forms.
Just to tag onto this comment, the "on" value is the value that the browser gives when there is no value attribute on the input field. So you can set value="on" to be 1, true, yes, or whatever.
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 04-20-2010, 05:35 AM Re: Unchecking a Checkbox
theinc's Avatar
Junior Talker

Posts: 2
Name: Nigel Smithinks
Trades: 0
You better echo the value ,
Quote:
checked=checked
or else you might get some errors
__________________
Flexible server
Please login or register to view this content. Registration is FREE
services and
Please login or register to view this content. Registration is FREE
rack space.
theinc is offline
Reply With Quote
View Public Profile
 
Old 04-20-2010, 07:46 AM Re: Unchecking a Checkbox
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
actually it should be

PHP Code:
echo(' checked=\"checked\" '); 
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?

Last edited by chrishirst; 04-20-2010 at 08:06 AM..
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 04-20-2010, 08:25 AM Re: Unchecking a Checkbox
Extreme Talker

Posts: 246
Trades: 3
Actually, if you are using single quotes in PHP, there's no need to escape the double quotes.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
CouponGuy is offline
Reply With Quote
View Public Profile
 
Old 04-20-2010, 08:27 AM Re: Unchecking a Checkbox
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
True, but it doesn't hurt owt
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 04-20-2010, 10:13 AM Re: Unchecking a Checkbox
Extreme Talker

Posts: 246
Trades: 3
Chris, you're the last person I want to argue with, but while that does "work", it works as well as putting anything after checked=, including, but not limited to:
  • off
  • "off"
  • \"off\"
  • [nothing]
  • garbage
  • "Chris is cool!"

What doesn't work so well is escaping double quotes around checkbox or some other important value, when single quotes are used around the entire string, such as:
PHP Code:
echo('<input type=\"checkbox\">'); 
{Note: the above does not result in a checkbox input}
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
CouponGuy is offline
Reply With Quote
View Public Profile
 
Old 04-20-2010, 10:51 AM Re: Unchecking a Checkbox
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
I'll have to bow to superiour knowledge on the single quote and escaping double quotes. Maybe I should do more PHP programming

Certainly any string after checked= will trigger a true condition in browsers and if you are using a "loose" DTD the quotes around the value could be left off.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 04-22-2010, 11:58 PM Re: Unchecking a Checkbox
portkey's Avatar
Skilled Talker

Posts: 55
Name: Erica Dion
Trades: 0
Thanks for all the posts but sadly I still don't have a fix. I don't think there's an issue with the php portion because I use the identical code when adding a new entry to the database. It's when I edit an entry, the HTML can't seem to recognize that it's unchecked. I tried using javascript to uncheck the box but that still didn't do anything. I'm completely stumped!

It's always "on" for the values that were checked by default
Code:
$_POST["M"] = on
I tried adding a string to the checked value but that didn't seem to change anything
__________________
Hysterical Web Design

Please login or register to view this content. Registration is FREE

Fair priced and reliable web design services that complement your website needs

Last edited by portkey; 04-23-2010 at 12:06 AM..
portkey is offline
Reply With Quote
View Public Profile Visit portkey's homepage!
 
Old 04-23-2010, 02:47 PM Re: Unchecking a Checkbox
portkey's Avatar
Skilled Talker

Posts: 55
Name: Erica Dion
Trades: 0
I think I might have figured it out but before I do all the work, I'm hoping someone can confirm if this is true... Hopefully I can explain it well enough.

The page is a schedule page, similar to Microsoft's task scheduler. It uses "invisible" divs and javascript to switch between daily, weekly, and monthly. Now weekly and monthly have the same set of checkboxes for Sunday through Saturday. Only one set displays at a time though.

My first question is, if the Div is set to "display:none;", will the checkboxes still read on the next page?

Question two, if question one is true, and there are actually two checkboxes for Monday(M) let's say, if one is true, is that all that's needed? So like a digital truth table:
FALSE + FALSE = FALSE
FALSE + TRUE = TRUE
TRUE + TRUE = TRUE

Am I right in my thinking? Thank you! I realize this probably more of an HTML/CSS question now but I appreciate it!
__________________
Hysterical Web Design

Please login or register to view this content. Registration is FREE

Fair priced and reliable web design services that complement your website needs

Last edited by portkey; 04-23-2010 at 02:48 PM..
portkey is offline
Reply With Quote
View Public Profile Visit portkey's homepage!
 
Old 04-23-2010, 07:01 PM Re: Unchecking a Checkbox
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
As for the first question, I'm not sure - That is a good question. One thing you can do to avoid that confusion is to have a unique submit button for each section.

For the second question, you can use the OR operator. Example:

if (FALSE OR FALSE) ... equals false
if (FALSE OR TRUE) ... equals true
if (TRUE OR TRUE) ... equals true
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 04-23-2010, 08:47 PM Re: Unchecking a Checkbox
portkey's Avatar
Skilled Talker

Posts: 55
Name: Erica Dion
Trades: 0
Thanks, mgraphic. I ended up trying out my theory by changing the names between the two sets. Turns out even if the div is invisible, the checkbox is still registered in the page so I technically had two of every checkbox and it really threw it off. So my last post was correct, as long as one checkbox with a given name is checked, it will be TRUE. All checkboxes with the same name must be unchecked in order for it to be 'off'. This is why I like computer programming more than web, lol
__________________
Hysterical Web Design

Please login or register to view this content. Registration is FREE

Fair priced and reliable web design services that complement your website needs

Last edited by portkey; 04-23-2010 at 08:50 PM..
portkey is offline
Reply With Quote
View Public Profile Visit portkey's homepage!
 
Reply     « Reply to Unchecking a Checkbox
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.67508 seconds with 12 queries