Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    HTML Checkbox - Validation Problem

    I have a page that has a checkbox. The checkbox is initially checked/unchecked depending on a value in a table. (Of course, the user has the ability to change this value once the page loads, this is just used for the initial value).

    The following code works, but does not validate using XHTML 1.0 STRICT:

    Code:
    <input type = 'checkbox' Name="chkComplete <?PHP print $chkComplete; ?> >Would you like to mark the item as completed?
    I know to validate to XHTML strict, I would need to code as either checked="checked" or unchecked="unchecked". How can I incorporate this into the PHP print or echo command so that the line will validate?

    Thanks.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,866
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    What's that $chkComplete; ?

    The following should validate.
    Code:
    <input type = "checkbox" name="chkComplete" checked="checked" />
    Last edited by abduraooft; 06-18-2009 at 04:59 PM. Reason: missed the 'Name' in camel case
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Earlier in the code, I'm reading a table to determine if there is a 'completed date' populated.

    If so, I check the checkbox initially. If there is no date, I do not initially check the checkbox.

    I'm using the following assignment to accomplish this:

    PHP Code:
                    if ($Completed_Date ' ')
                        
    $chkComplete 'checked';
                    else
                        
    $chkComplete 'unchecked'
    This works as far as checking/unchecking the checkbox, but it doesn't validate.

    Perhaps I'm doing something crazy here and it should be much more simple than this?

    Thanks.

    Quote Originally Posted by abduraooft View Post
    What's that $chkComplete; ?

    The following should validate.
    Code:
    <input type = "checkbox" Name="chkComplete" checked="checked" />

  • #4
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,928
    Thanks
    6
    Thanked 1,041 Times in 1,014 Posts
    1. XHTML code must be written all lowercase. Look at your name attribute.
    2. There is no “unchecked” state or attribute. Unchecked is the default so if it’s not checked then just display nothing at all.


    Also it would help if you showed us the validator results (i. e. to see what exactly the validator says isn’t validating).

    And finally I would recommend:
    Code:
    <input … id="inp_completed" /><label for="inp_completed">Would you like to mark the item as completed?</label>
    Last edited by VIPStephan; 06-18-2009 at 04:26 PM.

  • #5
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,313
    Thanks
    4
    Thanked 207 Times in 204 Posts
    As shown to have it checked you need to use

    Code:
    checked="checked"
    so in your PHP use:

    PHP Code:
    $chkComplete '';
    if (
    $Completed_Date ' ') {$chkComplete 'checked="checked"';} 
    It's also important to close your input tag using /> as abduraooft showed you.
    Dave .... HostMonster for all of your hosting needs

  • #6
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    All noted.

    Thank you!

    Quote Originally Posted by VIPStephan View Post
    1. XHTML code must be written all lowercase. Look at your name attribute.
    2. There is no “unchecked” state or attribute. Unchecked is the default so if it’s not checked then just display nothing at all.


    Also it would help if you showed us the validator results (i. e. to see what exactly the validator says isn’t validating).

    And finally I would recommend:
    Code:
    <input … id="inp_completed" /><label for="inp_completed">Would you like to mark the item as completed?</label>

  • #7
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ha! Of course.

    I was so focused on the HTML, I didn't even THINK about changing the PHP.

    Thanks!

    Quote Originally Posted by djm0219 View Post
    As shown to have it checked you need to use

    Code:
    checked="checked"
    so in your PHP use:

    PHP Code:
    $chkComplete '';
    if (
    $Completed_Date ' ') {$chkComplete 'checked="checked"';} 
    It's also important to close your input tag using /> as abduraooft showed you.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •