Unchecked checkbox still posting as checked – PHP

  php

Q(Question):

This is embarrassing, but I have a form pulling data from mysql using php. Lots of different checkboxes in it. When user checks one or more boxes, all goes well; values are added and db is updated. And some php script makes sure those same checkboxes are then checked when page is reloaded. Here’s the problem:

if I now uncheck one or more of those (automatically when the page loads) checked checkboxes and then do a submit, the form still treats those unchecked checkboxes as if they were checked. I have spent 6 solid hours going over and over my code, googling for answers and am now officially stuck. I’m sure this is some amateur newbie error. Can someone put me out of my misery? Code is below.

To update the data


foreach($_POST as $key=>$value)
{
if(isset($key) && $key!="updatehr" && $key!="hwdates" && $key!="hrid" && $key!="hrclass")
{
if(substr_count($key,"hrreg"))
{
$keys=explode('hrreg',$key);
$studid=$keys[0];
$hrregdate=$keys[2];
$hrregitem=$keys[1];
$q="insert into hrregular values ('','$hrid','$studid','$hrregdate','$hrregitem')";
$r=mysql_query($q) or die(mysql_error());
}
}
}

for($j=1;$j<=20;++$j)
{
echo "<td>";
$q6="select hrregid from hrregular where studid='".$sid."' and hrregdate='".$dates[floor(($j-1)/4)]."' and hrregitem='".$arr1[($j-1)]."'";
$r6=mysql_query($q6) or die(mysql_error());
echo "<input style='margin-top:0px;' type=checkbox name='".$sid."hrreg".$arr1[($j-1)]."hrreg".$dates[floor(($j-1)/4)]."' ";
if(mysql_numrows($r6)==1)
{
echo " checked ";
}
echo "></td>";
}

A(Answer):

Heya, Beary.

Do a print_r() on $_POST to make sure you’re submitting what you think you’re submitting.

A(Answer):

Hey there,

Yep done that and lot’s more. I wanted to be really sure before I posted. The only problem in the whole thing is that when the page (re-)loads it has certain boxes checked. If I uncheck them and then submit, it acts as if I hadn’t unchecked them.

Heya, Beary.

Do a print_r() on $_POST to make sure you’re submitting what you think you’re submitting.

A(Answer):

Maybe I’m just not looking closely enough, but I didn’t see any UPDATE statements in there. When you are editing the data, you will need to UPDATE. It looks like you might be INSERTing it again. So you’d be creating a new record with the new data, and not actually changing the original record.

A(Answer):

No you were looking closely enough. Instead of updating, I’m deleting relevant records and then inserting new ones. In this case, it’s stacks easier to code and far less messy.

Maybe I’m just not looking closely enough, but I didn’t see any UPDATE statements in there. When you are editing the data, you will need to UPDATE. It looks like you might be INSERTing it again. So you’d be creating a new record with the new data, and not actually changing the original record.

A(Answer):

No you were looking closely enough. Instead of updating, I’m deleting relevant records and then inserting new ones. In this case, it’s stacks easier to code and far less messy.

Really?

I’ve had to maintain systems coded like this in a variety of languages and it’s never easier to code, it’s always more messy from a data point of view and it can, depending on the DB in question slow the application down massively over time. I’d really re-think this idea.

As for the check box issue, first I admit I haven’t thoroughly read all the posts or the code you gave but have you tried a simple peice of JS on the onclick? I have in the past issued code like:


onclick = "this.value=this.checked;"

That then sets the value for you, you can do things more complicated such as:


onclick="this.value=(this.checked)?1:0;"

It might help, and you should rethink that data strategy it’s asking for trouble long term.

Cheers
nathj

A(Answer):

Thanks nathj for the thoughts. I will have another look at it all. But in case anyone’s interested, I have discovered the reason it’s falling. It’s actually nothing to do with the code or the sql or anything like that. These checkboxes are stored in a table which has javascript at the end of the table to convert the table to a scrolling table. It works wonderfully and you can set the number of fixed columns and headers as you want. But I discovered that this js is preventing the unchecked checkboxes from being registered as unchecked. When I took away the js scrolling table conversion, everything worked as desired and it registered the unchecked checkboxes as unchecked.

So, I now know why it’s not behaving but am no closer to making it work and maintaining the scrolling facility. If anyone’s read this far and is interested in the scrolling table, it’s at http://www.matts411.com/post/super_tables/.

I plan on emailing the author to see if he’s aware of this and if it can be overcome.

Really? ……


onclick="this.value=(this.checked)?1:0;"

It might help, and you should rethink that data strategy it’s asking for trouble long term.

Cheers
nathj

LEAVE A COMMENT