Results 1 to 5 of 5

Thread: Web Form Processing a Checkbox

  1. #1
    Join Date
    Sep 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Web Form Processing a Checkbox

    Hi there

    Further to my post yesterday about processing webform, thanks I now have it working (mostly).

    But I'm having problems processing a check box.

    I just need to set the variable to 'checked' or 'unchecked' for the checkbox.

    Here my code so far.

    $ch1 = 'unchecked';

    if ($ch1 = 'yes') {
    $ch1 = 'checked';
    }

    if ($ch1 == 'checked') {
    $ch1 = 'The checkbox is checked';
    }
    else
    {
    $ch1 = 'The checkbox is unchecked';
    }

    At the moment the vaule return is 'checked' whether the checkbox is actually ticked or not (always returns true).

    Thanks, James

  2. #2
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    There is an error with your logic, you are setting the value of $ch1 to always be "unchecked" then testing if it is "yes".

    PHP Code:
    $ch1 'unchecked';

    if (
    $ch1 'yes') {
    $ch1 'checked';

    Take out the first line and change the second part like this:

    PHP Code:
    if ($ch1 == 'yes') {
    $ch1 'checked';

    The other thing you should do if that doesn't work is to echo the value of $ch1 and then test it to see what it contains when the checkbox is checked and when it is not.

  3. #3
    Join Date
    Sep 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    HI

    Thanks for your advice, I have since modified the code but still having problems getting it to work.

    $ch1 = $_POST['thechkbox'];

    if ($ch1 == 'checked') {
    $ch1 = 'checked';
    }
    else
    {
    $ch1 = 'no not checked';
    }

    If I leave the checkbox unticked I get the following error:
    Undefined index: thechkbox
    IF I tick the checkbox I get no error.

    Any pointer would be greatly appreciated.

    Thanks again, James
    Last edited by jamez100; 09-22-2009 at 02:54 PM.

  4. #4
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    Can you post the HTML for the checkbox?

    You probably need to set a default value for the checkbox or have a validation process that ensures that the field has been set.

  5. #5
    Join Date
    Nov 2008
    Posts
    58
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Default

    use the isset() function.

    PHP Code:
    if(isset($_POST['thechkbox']) &&
    $_POST['thechkbox'] == 'checked')
    {
     

    The tutorial in the page below might help:
    checkbox in a PHP form

Bookmarks

Posting Permissions

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