Solved

keep the current checkboxes checked after submit

Posted on 2010-08-30
7
2,497 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 108

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 108

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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now