Forum Moderators: coopster

Message Too Old, No Replies

Need help maintaining checkbox values when sorting

I have a number of columns, when I sort on one, I lose my checkbox values.

         

asurkis

5:01 am on May 3, 2010 (gmt 0)

10+ Year Member



I am using php to code a page with a form that has multiple checkboxes. Each checkbox is associated with a number of pieces of data, so I have a column of checkboxes, followed by several columns of data. I want to be able to sort on different columns, but when I do, it resets my checkboxes. I don't think I can use POST because I am not submitting the form at this point, just sorting. When I have tried to use SESSION, I've had no luck -- I don't understand how/when the information about which boxes are checked can be put into the SESSION variable. I've been banging my head against this problem for over a week -- any help appreciated!

omoutop

10:25 am on May 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



welcome to webmasterworld asurkis

To my opinion (and knowledge) $_POST is the only way to do it. You just have to post your data to an ajax proccess script (for the sorting) before the actual posting to your main script

Matthew1980

10:31 am on May 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there asurkis,

Welcome to the forum!

Are you getting the information from a db at all?

Or could you post a portion of the code and from there we can be more helpful.

Cheers,
MRb

asurkis

12:28 pm on May 3, 2010 (gmt 0)

10+ Year Member



Thanks for the replies

omoutop -- unfortunately I'm not at all familiar with ajax process scripts -- I'm very new to all this -- can you give me any more pointers in that direction?

Matthew1980 -- I am not using a db. below is the basics of my code -- I'm reading in a bunch of XML files and then putting the information into arrays ($FileIDs, $CaseLabels, $Ages, $Genders) and sorting based on user clicking on column heading/link. So what I need is to be able to keep track of the value of checked[] below.


if (isset($_GET["sort"])) {
$sortVar = $_GET["sort"];
}

if ($sortVar == "Age") {
array_multisort($Ages,$CaseLabels,$Genders,$FileIDs);
} else if ($sortVar == "Gender") {
array_multisort($Genders,$Ages,$CaseLabels,$FileIDs);
} else {
array_multisort($CaseLabels,$Ages,$Genders,$FileIDs);
}

echo <<<EOF
<form action="summaries.php" method="post">
<table width="100%">
<tr>
<th/>
<th><a href="index.php"><h3>CaseLabel</h3></th>
<th><a href="index.php?sort=Age"><h3>Age</h3></th>
<th><a href="index.php?sort=Gender"><h3>Gender</h3></th>
</tr>
EOF;
for ($i = 0; $i < $numCases; $i+=1) {
echo <<<EOF
<tr>
<td><input type="checkbox" value="$FileIDs[$i]" name=checked[]/></td>
<td>{$CaseLabels[$i]}</a></td>
<td>{$Ages[$i]}</a></td>
<td>{$Genders[$i]}</td>
</tr>
EOF;
}
echo '<tr><td width="16%" align="left"><input type="submit" name="formSubmit" value="Summaries"/></tr>';
echo '</table>';
echo '</form>';

Matthew1980

4:47 pm on May 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there asurkis,

Are you saying as you want the values (if selected) to be remembered if you reorder the list from clicking on any one of the headings? If so that's quite tricky, $_SESSION may be a way if you don't know much/any ajax. But I have never tried to do anything like that before.

One suggestion I do have however is making your code a little more secure:-

if (isset($_GET['sort'])) {
$sortVar = strip_tags($_GET['sort']);
}
else{
$sortVar = "default value here";
}


Strip anything off the input that could be harmful, and assign a default value to the $sortVar in case somebody tries something they shouldn't, at least if you have an else on there it *should* prevent misuse.

Also: for ($i = 0; $i < $numCases; $i+=1) shouldn't $i be $i++; there? Or have I missed something obvious because to me that says $i = 1, then if it's less than $numCases Add 1 to $i making $i only ever equal 1..

Other than that I'm not too sure, as I have not attempted anything like that before.

Cheers,
MRb

rocknbil

4:50 pm on May 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



but when I do, it resets my checkboxes.


I don't use the array method checkbox[], I use named checkboxes. Convert to the method of choice.


echo "
<tr>
<td><input type=\"checkbox\" value=\"" . $FileIDs[$i] . "\" name=\"$the_name\"";
if (isset($_POST[$the_name])) { echo ' checked="checked"'; }
// I see you're using XHTML for whatever reason, <= HTML 4.01 is just ' checked';
echo <<< EOF /></td>
<td>{$CaseLabels[$i]}</a></td>
<td>{$Ages[$i]}</a></td>
<td>{$Genders[$i]}</td>
</tr>
EOF;

asurkis

4:58 pm on May 3, 2010 (gmt 0)

10+ Year Member



Thanks Matthew1980 -- I'll add that code to make it more secure. I have tried with $_SESSION, but the problem is that the checkbox values need to somehow be passed to $_SESSION when the user sorts, but I don't know how/where to insert my code to grab those values and set $_SESSION at that moment.

asurkis

5:01 pm on May 3, 2010 (gmt 0)

10+ Year Member



Hi rocknbil -- thanks for the response, not sure I understand it though. My question is how will the value of $the_name get into $_POST without my hitting the submit button?