Solved

keep the current checkboxes checked after submit

Posted on 2010-08-30
7
2,586 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
preventing spam with mailto 22 31
How would I do a website like this? 5 57
Creating Functions in phpMyAdmin 8 17
How is this connection happening? 3 15
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

778 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