Search code examples
phphtmlcomputer-sciencehtml-formhandler

PHP / HTML checkbox button


I have a GPA calculator form, and when a user enters their information, I want it to stay populated on the screen.

    E-mail: <input type="text" size="35" name="email" value="<?php echo $email; ?>">
    <span class="error"><?php echo $emailErr; ?></span>
    <br><br>

    <input type="checkbox" name="agree" >
    I agree to the terms and conditions of this website.
    <span class="error"><?php echo $agreeErr; ?></span>
    <br><br>

I have no problem with my other variables, but I cannot get my checkbox to stay on the screen. When the user hits submit, the checkbox will un-check again. How can I make it stay checked on the screen?

Thank you!


Solution

  • Through trial and error, I have found a simple solution:

    <input type="checkbox" name="agree" 
    value="agree" <?php if(isset($_POST['agree'])) echo "checked='checked'";?> 
    >I agree to the terms and conditions of this website.
    <span class="error"><?php echo $agreeErr; ?></span>
    
    

    Just in case anyone else gets stuck on this same problem!