skip to Main Content

I’m trying to make a simple contact form, but I have difficulty to understand how checkbox works. This is the part I have a problem:

HTML:

<input name="selection" type="checkbox">

PHP:

$selection = $_POST['selection'];
$mail->Body = "
    Answer is ".$_POST["selection"]."
"

Once the checkbox is clicked and the form is submitted, the function above prints "Answer is on" in the e-mail.
How can I print "Answer is Positive" once checkbox is clicked, and "Answer is Negative" when checkbox is not clicked in a simple way?

2

Answers


  1. Chosen as BEST ANSWER

    Alright, I didn't know that defining a custom positive value for a checkbox is so simple:

    HTML

    <input name="selection" type="checkbox" value="Write here what you want to post when the checkbox is checked">
    

    As for posting the negative value, I realized that it's not that necessary for my project. For those who wonder how, all alternatives are explained here:

    POST unchecked HTML checkboxes


  2. This is what I’ve tried, I don’t know if it solves your question…

    
    if(isset($_GET['answer']))
    {
        echo "Answer is correct.";
        // more details here
    }
    else
    {
        echo "Answer is incorrect.";
    }
    
    
    ?>
    
    <form action="/" method="GET">
        
        <label for="answer">
            <input type="checkbox" name="answer" id="lol" onchange="this.form.submit()" <?=$_GET['answer'] == "on" ? "checked" : ""?>>
            Correct?
        </label>
    </form>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search