Checkbox problem in PHP

Discussion in 'PHP' started by inderpal, Aug 29, 2006.

  1. #1
    Hi All,

    I have a checkbox in HTML page.

    <input type="checkbox" name="searchMe">
    Code (markup):
    When I submit the form and retrieve the value in PHP
    $strSearchMe = $_REQUEST['searchMe'];
    Code (markup):
    If the checkbox is checked then its working fine but If it's not checked then I am getting an error

    Notice: Undefined index: searchMe in C:\Inetpub\wwwroot\jobhob\registeremployers.php on line 28

    I dont understand why this error is coming. I would appreciate any help in this regard. Thanks in advance!

    Best Regards,
    Inderpal Singh
     
    inderpal, Aug 29, 2006 IP
  2. Cryogenius

    Cryogenius Peon

    Messages:
    1,280
    Likes Received:
    118
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think it's because the array element 'searchMe' is not set if the checkbox is not selected. Use the isset() function to see if it has been set, like this:

    
    if (isset($_REQUEST['searchMe'])) {
        $strSearchMe = $_REQUEST['searchMe'];
    } else {
        $strSearchMe = false;
    }
    
    PHP:
    Cheers, Cryo.
     
    Cryogenius, Aug 29, 2006 IP
    ahkip likes this.
  3. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Cryogenius is correct: it's part of the HTML spec that checkbox values are not sent if they are not checked. It's not a problem with PHP as such, it's a 'problem' with the HTML spec.
     
    TwistMyArm, Aug 29, 2006 IP
  4. Cryogenius

    Cryogenius Peon

    Messages:
    1,280
    Likes Received:
    118
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks for the confirmation TwistMyArm... I wasn't 100% sure.

    Cryo.
     
    Cryogenius, Aug 29, 2006 IP
  5. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #5
    You can also omit warning by using @:

    $strSearchMe = @$_REQUEST['searchMe'];
    PHP:
    If checkbox is not checked, $strSearchMe will be empty.
     
    wmtips, Aug 29, 2006 IP
    ahkip and Cryogenius like this.
  6. inderpal

    inderpal Active Member

    Messages:
    919
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    68
    #6
    Finally it's working. Thanks all for your help.

    Best Regards,
    Inderpal Singh
     
    inderpal, Aug 30, 2006 IP
  7. mariush

    mariush Peon

    Messages:
    562
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I'm doing something like this :

    
    $value=$_REQUEST["checkbox"];
    if ((!$value) || ($value=="") ) $value=0;
    
    PHP:
     
    mariush, Aug 30, 2006 IP
  8. void

    void Peon

    Messages:
    119
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I usually use the following trick.

    <input type="hiddden" name="searchMe" value="0">
    <input type="checkbox" name="searchMe" value="1">
    HTML:
    Only the last value is sent, so if the checkbox is checked 1 is sent, otherwise 0 is sent. Simplifies the PHP as there's always a value.
     
    void, Aug 30, 2006 IP