Display a checked checkbox in a form to form POST

2019-08-30 23:18发布

问题:

I have this wizard that needs to display the previous posted value

index.html

<form method="post" action="posted.php">
<input type="text" name="surname" value="" placeholder="Surname" />
<input type="text" name="firstname" value="" placeholder="Firstname" />
<input type="checkbox" name="php" />
<input type="checkbox" name="jquery" />
<input type="checkbox" name="python" />
<input type="submit" value="Submit" />
</form>

in the posted.php i have a similar form only this time i know the value from $_POST

   <form method="post" action="finish.php">
    <input type="text" name="surname" value="<?php echo $_POST['surname']; ?>" placeholder="Surname" />
    <input type="text" name="firstname" value="<?php echo $_POST['firstname']; ?>" placeholder="Firstname" />
    <input type="checkbox" name="php" />
    <input type="checkbox" name="jquery" />
    <input type="checkbox" name="python" />
    <input type="submit" value="Submit" />
    </form>

I am having a hard time trying to come up with a solution that shows what checkbox was checked.I have seen several solutions like https://stackoverflow.com/a/11424091/1411148 but i wondering if there more solution probably in html5 or jquery.

How can i show what checkbox was checked?.The probem i am having is that <input type="checkbox" name="jquery" checked /> checked checks the checkbox and no post data can be added to show what the user checked.

回答1:

This would be a way to go:

<form method="post" action="finish.php">
    <input type="text" name="surname" value="<?php echo $_POST['surname']; ?>" placeholder="Surname" />
    <input type="text" name="firstname" value="<?php echo $_POST['firstname']; ?>" placeholder="Firstname" />
    <input type="checkbox" name="php" <?php if (isset($_POST['php'])) echo 'checked="checked"'; ?> />
    <input type="checkbox" name="jquery" <?php if (isset($_POST['jquery'])) echo 'checked="checked"'; ?> />
    <input type="checkbox" name="python" <?php if (isset($_POST['python'])) echo 'checked="checked"'; ?> />
    <input type="submit" value="Submit" />
</form>