2

When I check the checkbox there is no errors, but when I didn't check the checkbox it gives me errors.

 <?php
    if(isset($_REQUEST['btn']))
    {
        $remmber = $_REQUEST['active'];
    if($remmber == "on")
        {  
            echo "Checked";
        }
        else  {$remmber = "";}
    }
    ?>
    <html>
    <form name= "frm" action = "test.php" method = "post" >
    <p>Username
    <input  type = "text" name = "name" value = ""  />
    </p>

    <p>Password
    <input type = "text" name = "password" value = "" />
    </p>

    <p>
    <td colspan = "2"><input type = "checkbox" name = "active" value = "active" />Keep Me Loged In 
    </p>

    <p><input type = "submit" name = "btn" value = "Login" />

    </p>


    </form>
    </html>
halfer
  • 19,824
  • 17
  • 99
  • 186
Akshat Dhiman
  • 61
  • 2
  • 9
  • possible duplicate of [Submit an HTML form with empty checkboxes](http://stackoverflow.com/questions/476426/submit-an-html-form-with-empty-checkboxes) – mrun Apr 29 '15 at 13:43

3 Answers3

7

For checkboxes if they are not checked then they are not posted. So check if it is present in the posted data then set on, else set blank to the variable. Try with -

if(isset($_REQUEST['btn']))
{
    $remmber = !empty($_REQUEST['active']) ? 'on' : '';
    if($remmber == "on")
    {  
        echo "Checked";
    }
    else 
        $remmber = "";
}
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
  • thanks for your help. Now its working but when i alredy used if else if it has on then its going to echo otherwise it is going to set $remember = "". than why it still giving errors. if($remmber == "on") { echo "Checked"; } else {$remmber = "";} – Akshat Dhiman Apr 29 '15 at 13:15
  • 2
    You should explain _why_ you need to do it this way for checkboxes. – Barmar Apr 29 '15 at 13:16
  • 1
    @AkshatDhiman See the other answer for why testing `== "on"` isn't working. – Barmar Apr 29 '15 at 13:16
  • @Barmar i am just using this for login page , where i am using a checkbox to remember username and password. – Akshat Dhiman Apr 29 '15 at 13:48
  • @AkshatDhiman I'm not sure what that has to do with why you should test whether `$remmber == "active"` instead of `$remmber == "on"` – Barmar Apr 29 '15 at 15:22
3

if($remmber == "on") yet you're using and specifying a value value = "active"

  • "on" is the default value for a checkbox if a value is not specified.

Therefore, you need to adjust it to read as

if($remmber == "active")

Edit: (an explanation)

The reason why you're getting an undefined index, is that once you hit the submit submit and do not tick the checkbox, it will produce that notice.

Modify your code to read as and check if the checkbox value is set.

if(isset($_REQUEST['btn']))
{

    if(isset($_REQUEST['active'])){

    $remmber = $_REQUEST['active'];

    if($remmber == "active")
        {  
            echo "Checked";
        }
        else  {$remmber = "";}

    } // closing brace for if(isset($_REQUEST['active']))

}

Additional edit, to show the user that the checkbox isn't set:

if(isset($_REQUEST['btn']))
{

    if(isset($_REQUEST['active'])){

    $remmber = $_REQUEST['active'];

    if($remmber == "active")
        {  
            echo "Checked";
        }
        else  {$remmber = "";}

    } // closing brace for if(isset($_REQUEST['active']))

    else{ echo "The checkbox was not ticked.";
    }

}

Footnotes:

  • You should also use a conditional !empty() for your inputs.

  • You're only checking if the submit and checkbox are set.


After noticing a comment you left in another answer:

@Barmar i am just using this for login page , where i am using a checkbox to remember username and password. – Akshat Dhiman

Please read the following Q&A on Stack:

It also contains valuable information regarding passwords.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

you can use simply isset

if(isset($_REQUEST['btn']))
{
    $remmber = isset($_REQUEST['active']) ? $_REQUEST['active'] : '';
    if($remmber == "on")
    {  
        echo "Checked";
    }
    else  {$remmber = "";}
}
Jayson
  • 1,105
  • 7
  • 25
  • 2
    Like I said in response to sgt BOSE's similar answer, you should explain _why_ this is necessary. – Barmar Apr 29 '15 at 13:20