By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,751 Members | 1,175 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,751 IT Pros & Developers. It's quick & easy.

Unchecked checkbox still posting as checked

100+
P: 170
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
Expand|Select|Wrap|Line Numbers
  1.     foreach($_POST as $key=>$value)
  2.         {
  3.         if(isset($key) && $key!="updatehr" && $key!="hwdates" && $key!="hrid" && $key!="hrclass")
  4.             {
  5.             if(substr_count($key,"hrreg"))
  6.                 {
  7.                 $keys=explode('hrreg',$key);
  8.                 $studid=$keys[0];
  9.                 $hrregdate=$keys[2];
  10.                 $hrregitem=$keys[1];
  11.                 $q="insert into hrregular values ('','$hrid','$studid','$hrregdate','$hrregitem')";
  12.                 $r=mysql_query($q) or die(mysql_error());
  13.                 }    
  14.             } 
  15.         }
  16.  
Expand|Select|Wrap|Line Numbers
  1.         for($j=1;$j<=20;++$j)
  2.             {
  3.             echo "<td>";
  4.             $q6="select hrregid from hrregular where studid='".$sid."' and hrregdate='".$dates[floor(($j-1)/4)]."' and hrregitem='".$arr1[($j-1)]."'";
  5.             $r6=mysql_query($q6) or die(mysql_error());
  6.             echo "<input  style='margin-top:0px;' type=checkbox name='".$sid."hrreg".$arr1[($j-1)]."hrreg".$dates[floor(($j-1)/4)]."' ";
  7.             if(mysql_numrows($r6)==1)
  8.                 {
  9.                 echo " checked ";
  10.                 }
  11.             echo "></td>";
  12.             }    
  13.  
Oct 10 '08 #1
Share this Question
Share on Google+
6 Replies


pbmods
Expert 5K+
P: 5,821
Heya, Beary.

Do a print_r() on $_POST to make sure you're submitting what you think you're submitting.
Oct 11 '08 #2

100+
P: 170
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.
Oct 11 '08 #3

P: 44
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.
Oct 12 '08 #4

100+
P: 170
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.
Oct 13 '08 #5

nathj
Expert 100+
P: 938
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:
Expand|Select|Wrap|Line Numbers
  1. onclick = "this.value=this.checked;"
  2.  
That then sets the value for you, you can do things more complicated such as:
Expand|Select|Wrap|Line Numbers
  1. onclick="this.value=(this.checked)?1:0;"
  2.  
It might help, and you should rethink that data strategy it's asking for trouble long term.

Cheers
nathj
Oct 13 '08 #6

100+
P: 170
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? ......


Expand|Select|Wrap|Line Numbers
  1. onclick="this.value=(this.checked)?1:0;"
  2.  
It might help, and you should rethink that data strategy it's asking for trouble long term.

Cheers
nathj
Oct 14 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.