Solved

keep the current checkboxes checked after submit

Posted on 2010-08-30
7
2,409 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
Comment Utility
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
Comment Utility
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
Comment Utility
Interesting question: Do you want the checkboxes to be remembered or do you want them to be changeable on each page?
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
thanks
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
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.

771 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

11 Experts available now in Live!

Get 1:1 Help Now