Solved

keep the current checkboxes checked after submit

Posted on 2010-08-30
7
2,833 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 110

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 110

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 110

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

751 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