SQL PHP HTML ASP JavaScript articles and free scripts to download If you are facing any problem in viewing this page, please tell us
 
 

Retaining checkbox value of a form after posting


Learn details on retaining all post back values of a form here

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

$t1=$_GET['t1'];
$t2=$_GET['t2'];

if($t1=="yes"){$t1v="checked";}
else{$t1v=="";}

if($t2=="yes"){$t2v="checked";}
else{$t2v=="";}

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 code of second page, pb-chk.php

$t1=$_POST['t1'];
$t2=$_POST['t2'];

header ("Location: pb-check.php?t1=$t1&t2=$t2");


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.

This is first checkbox
This is second checkbox


Further readings
All form components validation with post back and data locking
Checkbox
How to retain form data if validation fails?
How to retain data of a text box after submitting
Retaining status of a check box
Updating checkbox value from & to a table record.
Retaining selected data of a period button of a form
Retaining selected data of a drop down list box of a form
Handling drop down list box with multiple selection options using array
Validating Date: Checking if date exist
Email address validation in a form
Email validation using Ajax & PHP
PHP Form Validation
is_numeric to check numeric numbers
ctype_alnum to check alphanumeric characters data
ctype_alpha to check alphabetic characters data
How to take care of form & query string variables if Register global is OFF?















Sections
PHP
JavaScript
ASP
HTML
SQL
Photoshop
Articles SEO
PHP Tutorials
Form handling
PHP Monthly Planner
PHP Introduction
Loops & structure
Array
Date & Time
Functions
Form Handling
File Handling
Math Functions
String Functions
GD Functions
Comment Posting
Content Management
PHP & Ajax
Popular Tutorials
Drop down list
File Upload
Signup script
Member Login
Line Graph
PHP MySQL Paging
PHP Calendar
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.