Retaining checkbox value of a form after posting
We use checkbox in our forms and we will learn here how to post back or retain the value ( status ) of the checkbox. This tutorial is part of the series where we will try to understand how to retain value of every component of a form. You can read our main tutorial on how to retain value for component of a form here.
. We will use php header redirection to post back the values to our main page where checkbox is present. We will call form page as first page and action or submitted page as second page. The form data will be received by second page and after validation it is supposed to complete all the processing and in case of error it will post back the form data back to form page by using query string through address bar. To keep the second page simple we will keep only the redirection part. Here we have excluded the part which takes care if the form passes its validation and execute the actual script.
We will use the example of two checkbox but same system can be extended to more than two or single checkbox in a form.
Here is the code of a form with two checkboxes
Here is the code of second page, pb-chk.php
echo "<form method=post action=pb-chk.php>
<input type=checkbox name=t1 value=yes $t1v> This is first checkbox<br>
<input type=checkbox name=t2 value=yes $t2v> This is second checkbox
<input type=submit value=Submit>";
Here is the demo of the script with two checkboxes.This page will retain the status of the two checkboxes after submit. You can check or uncheck the value and test it.
header ("Location: pb-check.php?t1=$t1&t2=$t2");
Demo of retaining checkbox data
Be the first to post comment on this article :
|Click here for More on PHP File functions|
What is the most important factor of a web site