samedi 13 février 2021

Refresh a PHP page after checking/unchecking a checkbox

I'm pretty new with PHP, so help please. I need a web page in php with a checkbox. That page should refresh itself each time I do an action to the checkbox (so for both check or uncheck). Once it’s refreshed the page should keep the latest value of the checkbox. I tried the following example modifying another code I took from StackOverflow, but it doesn’t works as I wish. Any suggestion?

<?php
session_start();
$checked = "";


if($_SESSION['myaction'] != $_SESSION['value'])
{
    if(isset($_POST['sharks']))
    {
        $_SESSION['value'] = $_POST['sharks'];
    }
    else
    {
        $_SESSION['value'] = '';
        echo ":(";
    }
    $_SESSION['myaction'] = $_SESSION['value'];
}
?>

<form action="" method="POST">

<?php
print '<input name="sharks" type="checkbox" value="1" id="sharks" ';
if ($_SESSION['value'] == 1)
{
    echo "checked='checked'";
}
$myaction = 2;
print ">";
?>

</form>



Aucun commentaire:

Enregistrer un commentaire