Jump to content

[SOLVED] Checkbox Question


jdadwilson

Recommended Posts

I have a strange situation with Checkboxes that I need some assistance.

 

On a form I can have a variable number of checkboxes depending on information supplied by the client. Additionally, the checkboxes will have unique “values” and “label” depending on client input. Because of the way the POST action handles checkboxes I want to ensure the passed state is set when the page is reloaded.

 

First, in the login script I set the session variables, one for each checkbox like this...

 

$longNameA = explode{“|”, “Choose One|Name1|Name2|Name3”); // Sample only

$shortNameA = explode(“|”, “CO|N1|N2|N3”);  // Sample only

For ($i=1, $i<=count($shortNameA)-1, $i++)

{

$_SESSION[$shortNameA[$i]] = $shortNameA[$i];

}

 

Next, in my subject script I check to see if the action is coming from the forms “update” input like this:

 

if ($_POST['buttonID'] == "update")

{

For ($i=1, $i<=count($shortNameA)-1, $i++)

{

  $_SESSION[$shortNameA[$i]] =  (isset($_POST[groupCB[$shortNameA[$i]]) ? $_POST[groupCB[$shortNameA[$i]] : null);

}

// Set the $_POST values to the $_SESSION values

For ($i=1, $i<=count($shortNameA)-1, $i++)

{

  $_POST[groupCB[$shortNameA[$i]]] =  $_SESSION[$shortNameA[$i]);

}

 

// Finally set the Checkboxes

for ($i = 1; $i<=count($groupLongNameA)-1; $i++)

{ ?>

<input type="checkbox" name="groupCB[]" value="<?php echo $groupShortNameA[$i]; ?>"<?php if ($_POST[$groupCB[$i]] == $groupShortNameA[$i]) { echo " checked"; }?>><?php echo $groupLongNameA[$i]; ?> <?php

}?>

 

I think the logic of what I am doing is sound. The problem is that the coding is not functioning the way I would like. Being new to PHP I am not sure how to create and check the POST and SESSION variables the way that is required.

 

If you would like to see my work in progress send me an e-mail and I will provide you with the link.

 

TIA for any assistance.

 

Link to comment
Share on other sites

Couple...

 

1. When I initially load the page I would like to have all CBs checked. I have set the session variables during the login process to ensure this. This is not happening.

 

2. Once the state of a CB is changed any subsequent load of the page should reflect the last state. Thus, the reason for setting the session variables based on the post. Again, this is not happening.

 

Thanks for your assistance.

Link to comment
Share on other sites

I'm too tired to make much sense... sorry.

But I think there are a couple of errors in your code, although the idea is good.

 

I've actually just written a bit of code which should do near enough what you want.. I haven't tested it, As I've said I'm quite tired so hopefully I've not made too many mistakes ;)

<?php
$longNameA = explode{“|”, “Choose One|Name1|Name2|Name3”); // Sample only
$shortNameA = explode(“|”, “CO|N1|N2|N3”);  // Sample only

//I removed the code setting the session here, because I didn't think it was needed...
?>

Next, in my subject script I check to see if the action is coming from the forms “update” input like this:

<?php
if($_POST['buttonID'] == "update"){
//a shorter method of going through each checkbox that's been set and saving it into an array I've named checks
foreach($_POST['groupCB'] as $key => $value){
   		$_SESSION['checks'][] =  $value;
}
}

// Set the $_POST values to the $_SESSION values
for($i=1, $i<=count($shortNameA)-1, $i++){
	$_POST[groupCB[$shortNameA[$i]]] =  $_SESSION[$shortNameA[$i]);
}

// Finally set the Checkboxes
for($i = 1; $i<=count($groupLongNameA)-1; $i++){

	//here I've changed the check to do a simple search for the checboxes value in the session array.
	//if it's in the array then it checks it
	echo '<input type="checkbox" name="groupCB[]" value="' . $groupShortNameA[$i] . '"';
		if(in_array($groupShortNameA[$i], $_SESSION['checks'])){
			echo ' checked="checked"';
		}
	echo '>' . $groupLongNameA[$i] . ' ';
}
?>

 

I'm hoping that's all okay!

Link to comment
Share on other sites

Dragen;

 

You are the man!!!

 

Code worked great -- almost!

 

I added an unset($_SESSION['checks']; statement before the initial FOREACH loop. Without it if you check an unchecked box it would stay checked. But if you unchecked a checked box the box would get checked on a reload.

 

Additionally, I modified the one part that you removed (the part in the login script). Normally, I agree that CBs should not be checked. BUT, in this case I want them checked. So I added this code...

 

$groupShortNameA = explode("|", $groupShortName);

for ($i=1; $i<=count($groupShortNameA)-1; $i++)

{ $_SESSION['checks'][$groupShortNameA[$i]] = $groupShortNameA[$i]; }

 

Thanks much for your assistance.

 

The guys on this forum are the greatest...

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.