checkbox does not register PHP

Viewed 29

I am trying to make a form. I want to display a message if you have checked the checkbox and when submit it. If the checkbox is not checked, I do not want the massage.

I have tried a lot of things and I have searched a lot but can not make it work.

If I use 'isset', it will never display the message. Not when the box is checked and not when it is not checked. When I use '!isset' is will always display the message.

Can someone help me? Thanks

Code:

<?php
if ($_SERVER['REQUEST_METHOD']==='POST' ){

        if (isset($_POST['invoeren'])){
            echo'Invoer is gelukt';
        }
    }?>
    
<form>
<p>
    <label for="invoeren">wil je dit invoeren?</label>
    <input type="checkbox" id="invoeren" naam="invoeren">
 </p>
 <p>
    <input type="submit" value="submit">
 </p>
 </form>
1 Answers

You made a typo in the html tag. You should use the English name for the attribute instead of Dutch naam.

Also you should specify method='post' for the form. Otherwise it will use get as default.

<?php
if ($_SERVER['REQUEST_METHOD']==='POST' ){

        if (isset($_POST['invoeren'])){
            echo'Invoer is gelukt';
        }
    }?>
    
<form method='post'>
<p>
    <label for="invoeren">wil je dit invoeren?</label>
    <input type="checkbox" id="invoeren" name="invoeren">
 </p>
 <p>
    <input type="submit" value="submit">
 </p>
 </form>
Related