Message "Undefined index" when Checkbox is empty - php/html

Asked

Viewed 212 times

0

I have a problem that when the checkbox is empty, the message is displayed

Undefined index

I’ve seen some solutions on the Internet, so far in the same forum, but nothing has solved the problem.

Follow the Cód. below:

<!-- Cód. html -->
<input type="checkbox" name="cb">

//Cód. php
<?php 

$check_B = $_POST['cb'];

if(isset($check_B)){

    echo "Checkbox selecionado.";

} else { echo "Checkbox não selecionado.";
?>

Thank you!

1 answer

0


You should check whether $_POST['cb'] exists and not $check_B

<?php  

if(isset($_POST['cb'])){
    $check_B = $_POST['cb'];
    echo "Checkbox selecionado.";

} else {
    echo "Checkbox não selecionado.";
}
?>
  • Thank you Erlon! It worked right.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.