Solved

keep the current checkboxes checked after submit

Posted on 2010-08-30
7
2,654 Views
Last Modified: 2012-05-10
store checked values in a session

so when form is submitted

the current checkboxes are still checked
<SCRIPT LANGUAGE="JavaScript">
<!-- 

<!-- Begin
function Check(chk)
{
if(document.myform.Check_ctr.checked==true){
for (i = 0; i < chk.length; i++)
chk[i].checked = true ;
}else{

for (i = 0; i < chk.length; i++)
chk[i].checked = false ;
}
}

// End -->
</script>


echo "the following values have been checked"; 
<form name="myform" action="#" method="post">
<b>Scripts for Web design and programming</b><br>
<input type="checkbox" name="check_list" value="1">ASP<br>
<input type="checkbox" name="check_list" value="2">PHP<br>
<input type="checkbox" name="check_list" value="3">JavaScript<br>
<input type="checkbox" name="check_list" value="4">HTML<br>
<input type="checkbox" name="check_list" value="5">MySQL<br>

<input type="checkbox" name="Check_ctr" value="yes"
onClick="Check(document.myform.check_list)"><b>Check Control</b><br> 
<input type="submit">
</form>
<?
?>

Open in new window

0
Comment
Question by:rgb192
7 Comments
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33563741
Here's an example
<SCRIPT LANGUAGE="JavaScript">
<!-- 

<!-- Begin
function Check(chk)
{
if(document.myform.Check_ctr.checked==true){
for (i = 0; i < chk.length; i++)
chk[i].checked = true ;
}else{

for (i = 0; i < chk.length; i++)
chk[i].checked = false ;
}
}

// End -->
</script>


echo "the following values have been checked"; 
<form name="myform" action="#" method="post">
<b>Scripts for Web design and programming</b><br>
<input type="checkbox" name="check_list" value="1">ASP<br>
<input type="checkbox" name="check_list" value="2">PHP<br>
<input type="checkbox" name="check_list" value="3">JavaScript<br>
<input type="checkbox" name="check_list" value="4">HTML<br>
<input type="checkbox" name="check_list" value="5">MySQL<br>

<input type="checkbox" name="Check_ctr" value="yes"
onClick="Check(document.myform.check_list)"><b>Check Control</b><br> 
<input type="submit">
</form>
<?
?>

Open in new window

0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 33563746
That didn't paste properly
<SCRIPT LANGUAGE="JavaScript">
<!-- 

<!-- Begin
function Check(chk)
{
if(document.myform.Check_ctr.checked==true){
for (i = 0; i < chk.length; i++)
chk[i].checked = true ;
}else{

for (i = 0; i < chk.length; i++)
chk[i].checked = false ;
}
}

// End -->
</script>

<?php
echo "the following values have been checked: ";
$checked="";
$a = array();
if(isset($_POST["check_list"])) {
	foreach($_POST["check_list"] as $j=>$i) {
		$checked .= ($checked==""?"":",") . "checkbox" . $i;
		array_push($a, $i);
	}
}
echo $checked;

function was_checked($i,$a) {
if(in_array($i, $a)===true) {
return "checked='checked'";
return "";
}
}
?>

<form name="myform" action="#" method="post">
<b>Scripts for Web design and programming</b><br>
<input type="checkbox" name="check_list[]" value="1" <?=was_checked(1,$a)?>>ASP<br>
<input type="checkbox" name="check_list[]" value="2" <?=was_checked(2,$a)?>>PHP<br>
<input type="checkbox" name="check_list[]" value="3" <?=was_checked(3,$a)?>>JavaScript<br>
<input type="checkbox" name="check_list[]" value="4" <?=was_checked(4,$a)?>>HTML<br>
<input type="checkbox" name="check_list[]" value="5" <?=was_checked(5,$a)?>>MySQL<br>

<input type="checkbox" name="Check_ctr" value="yes"
onClick="Check(document.myform.check_list)"><b>Check Control</b><br> 
<input type="submit">
</form>

Open in new window

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 33566370
Interesting question: Do you want the checkboxes to be remembered or do you want them to be changeable on each page?
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 109

Expert Comment

by:Ray Paseur
ID: 33566384
This will remember them permanently - any check box that gets checked will be remembered across multiple pages.
<?php // RAY_checkboxes_session.php
error_reporting(E_ALL);
session_start();

// INITIALIZE SOME TEST DATA - THIS MIGHT COME FROM A DATA BASE
if (!isset($_SESSION['box1'])) $_SESSION['box1'] = FALSE;
if (!isset($_SESSION['box2'])) $_SESSION['box2'] = FALSE;
if (!isset($_SESSION['box3'])) $_SESSION['box3'] = FALSE;

// PROCESS THE POST DATA
if (isset($_POST['box1'])) $_SESSION['box1'] = TRUE;
if (isset($_POST['box2'])) $_SESSION['box2'] = TRUE;
if (isset($_POST['box3'])) $_SESSION['box3'] = TRUE;

// CREATE THE FORM
$form = <<<EOFORM
<form method="post">
CHECK OR UNCHECK SOME BOXES HERE<br/>
<input name=box1 type="checkbox" /><br/>
<input name=box2 type="checkbox" /><br/>
<input name=box3 type="checkbox" /><br/>
<input type="submit" value="go" />
</form>
EOFORM;

// REPLACE THE FORM FIELDS TO INDICATE THE CHECKED VALUES IN THE SESSION
if ($_SESSION['box1']) $form = str_replace('box1', 'box1 checked', $form);
if ($_SESSION['box2']) $form = str_replace('box2', 'box2 checked', $form);
if ($_SESSION['box3']) $form = str_replace('box3', 'box3 checked', $form);

// PRESENT THE FORM
echo $form;

Open in new window

0
 

Author Closing Comment

by:rgb192
ID: 33566386
thanks
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 33566395
This will remember them, and allow them to be changed from page to page.
<?php // RAY_checkboxes_session.php
error_reporting(E_ALL);
session_start();

// INITIALIZE SOME TEST DATA - THIS MIGHT COME FROM A DATA BASE
if (!isset($_SESSION['box1'])) $_SESSION['box1'] = FALSE;
if (!isset($_SESSION['box2'])) $_SESSION['box2'] = FALSE;
if (!isset($_SESSION['box3'])) $_SESSION['box3'] = FALSE;

// PROCESS THE POST DATA
if ( isset($_POST['box1'])) $_SESSION['box1'] = TRUE;
if ( isset($_POST['box2'])) $_SESSION['box2'] = TRUE;
if ( isset($_POST['box3'])) $_SESSION['box3'] = TRUE;

if (!isset($_POST['box1'])) $_SESSION['box1'] = FALSE;
if (!isset($_POST['box2'])) $_SESSION['box2'] = FALSE;
if (!isset($_POST['box3'])) $_SESSION['box3'] = FALSE;

// CREATE THE FORM
$form = <<<EOFORM
<form method="post">
CHECK OR UNCHECK SOME BOXES HERE<br/>
<input name=box1 type="checkbox" /><br/>
<input name=box2 type="checkbox" /><br/>
<input name=box3 type="checkbox" /><br/>
<input type="submit" value="go" />
</form>
EOFORM;

// REPLACE THE FORM FIELDS TO INDICATE THE CHECKED VALUES IN THE SESSION
if ($_SESSION['box1']) $form = str_replace('box1', 'box1 checked', $form);
if ($_SESSION['box2']) $form = str_replace('box2', 'box2 checked', $form);
if ($_SESSION['box3']) $form = str_replace('box3', 'box3 checked', $form);

// PRESENT THE FORM
echo $form;

Open in new window

0
 
LVL 2

Expert Comment

by:Gatherer_Hade
ID: 33566424
This code properly stores checked values in session.  Also sets state of check control so it acts as expected.

Enjoy


<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function Check(chk)
{
	if(document.myform.Check_ctr.checked==true){
		for (i = 0; i < chk.length; i++)
			chk[i].checked = true ;
	}else{

		for (i = 0; i < chk.length; i++)
		chk[i].checked = false ;
	}
}

// End -->
</script>

<?php
//enable session
session_start();
if (count($_POST)){
	//save choices to session
        $_SESSION['check_list']=$_POST['check_list'];
}

function was_checked($i) {
  if ($_SESSION['check_list']){
	  if(in_array($i,$_SESSION['check_list']) ) {
	     return "checked='checked'";
	  }
  }
  return "";
}

function any_checked(){
  if (count($_SESSION['check_list'])){
	     return "checked='checked'";
  }
}
?>

<form name="myform" action="#" method="post">
<b>Scripts for Web design and programming</b><br>
<input type="checkbox" name="check_list[]" value="1" <?=was_checked(1)?>>ASP<br>
<input type="checkbox" name="check_list[]" value="2" <?=was_checked(2)?>>PHP<br>
<input type="checkbox" name="check_list[]" value="3" <?=was_checked(3)?>>JavaScript<br>
<input type="checkbox" name="check_list[]" value="4" <?=was_checked(4)?>>HTML<br>
<input type="checkbox" name="check_list[]" value="5" <?=was_checked(5)?>>MySQL<br>

<input type="checkbox" name="Check_ctr" value="yes" <?=any_checked()?> 
onClick="Check(document.myform.elements['check_list[]'])"><b>Check Control</b><br> 
<input type="submit">
</form>

<?php

if (count($_POST['check_list'])){
        echo "the following values have been checked"; 
	$check_list=$_POST['check_list'];
	foreach ($check_list as $check){
	  echo "<br> You checked $check";
	}
}
?>

Open in new window

0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question