Checkbox question

Discussion in 'PHP' started by enchance, Sep 21, 2007.

  1. #1
    How do I know if a checkbox has been clicked upon sending a formail? Something like the user will check a box if they want a copy of their message to be sent to them also. I'm not sure if this is a PHP question or a JavaScript one but if it's a JS one then my apologies.
     
    enchance, Sep 21, 2007 IP
  2. tamen

    tamen Peon

    Messages:
    182
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If it haven't been checked the variable $_POST['checkboxname'] will not be set.

    You can check that with:
    if (isset($_POST['checkboxname']))
    {
    echo 'Its set';
    } else {
    echo 'Its not set';
    }
     
    tamen, Sep 21, 2007 IP
  3. ErectADirectory

    ErectADirectory Guest

    Messages:
    656
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It's javascript if it happens before the form gets submitted to the php script, I believe I understand you and it seems you did post it to the right section.

    All you do is just check if the value of the box is equal to 'on' so...

    
    <?php
    if ($_POST['is_checked'] == 'on') {
      echo 'The box is checked.<br /><br />' ;
    }
    ?>
    <form action="" method="post">
      <input type="checkbox" name="is_checked">
      <input type="submit"
    </form>
    
    PHP:
    For some reason, checking to see if the box is not checked does not work like

    if ($_POST['is_checked'] == 'off') 
    PHP:
    Perhaps someone else can help with this part of it as I would just do

    if ($_POST['is_checked'] != 'on')
    PHP:
    which works perfectly fine.

    Hope this helps


     
    ErectADirectory, Sep 21, 2007 IP
  4. enchance

    enchance Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Good thing I posted it here then. So the string which is returned is 'on' I initially thought it would return a boolean value. I remember trying it and failing miserably. So, if there are multiple checkboxes you just have to go through each one, right? So if it's 'off' it doesn't return any value at all...that's weird.
     
    enchance, Sep 21, 2007 IP
  5. enchance

    enchance Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    If it doesn't return any value then it's pretty much easier to go through php
    
    <?php
    if(isset($_POST['checkboxname']))
    {
         //it was checked!
    }
    ?>
    
    Code (markup):
    Btw how come my code isn't color coded unlike yours? I'm jealous.
     
    enchance, Sep 21, 2007 IP
  6. ErectADirectory

    ErectADirectory Guest

    Messages:
    656
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Yes, that is bizarre and I really didn't realize that until reading tamen's post (he checks to see if the variable is set at all). It kind of sounds wrong to have a field in a form and it not return any value at all if it's not checked. Ooh well, that's programmer's logic for you.
     
    ErectADirectory, Sep 21, 2007 IP
  7. tamen

    tamen Peon

    Messages:
    182
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You could always install Firefox and download the LiveHTTPHeaders plugin and see what exactly gets send when you submit a form.
     
    tamen, Sep 21, 2007 IP
  8. enchance

    enchance Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Tamen, I dropped by the Firefox add-ons page. Would you suggest Header Spy 1.2 or Header Monitor 0.3? I'd rather use what you've used/are using.
     
    enchance, Sep 21, 2007 IP
  9. tamen

    tamen Peon

    Messages:
    182
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I use the one called Live HTTP Headers. You can find it here: http://livehttpheaders.mozdev.org/

    Its not pretty or anything and it will show you what gets send in the headers of any request made by Firefox. A godsend when you are automating anything on a webpage. Say, writing an automated Wordpress-account creator ;)
     
    tamen, Sep 21, 2007 IP
  10. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #10
    Use
     wrappers instead of [code].
    PHP:
     
    nico_swd, Sep 21, 2007 IP