473,396 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Unchecked checkbox still posting as checked

170 100+
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
6 3669
pbmods
5,821 Expert 4TB
Heya, Beary.

Do a print_r() on $_POST to make sure you're submitting what you think you're submitting.
Oct 11 '08 #2
beary
170 100+
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
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
beary
170 100+
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
938 Expert 512MB
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
beary
170 100+
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

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

Similar topics

1
by: Pete | last post by:
I have some session variables which get set on page one of my site. I can set an unset these by passing through a parameter in some URL's. When the user goes to the second page some checkboxes are...
1
by: Claire | last post by:
Hello, I am having a problem in my struts application with the checkboxes in my form. I have an array of checkboxes, some of which may be already selected when the form loads. My problem is when...
2
by: Joel Brabant | last post by:
Hi again, Within a submitted page that has mutilple checkboxes with the same name, the request.form("mycheckboxes") has a comma separated list of the values of all the ones checked.... but I...
3
by: Terence Parker | last post by:
I'm not sure whether this question demands a JavaScript answer or a simple HTML one - so apologies for the cross posting. I am grabbing a bunch of data from MySQL using PHP and returning the...
2
by: magmo | last post by:
Hi I have a checkbox column in my datagrid. When I uncheck the checkbox it turn grey, and then I have to uncheck it once more to get it completly unchecked. Why is that? I would like to skip...
2
by: Javier | last post by:
Hi Everyone, I have a dynamic checkbox in a datagrid that uses the ITemplate interface and has the checkchanged event wired up. When the checkbox is checked, the event event handler that...
4
by: Robert S | last post by:
I have a form with a checkbox: <form method="post" action="test.php"> <input type="checkbox" name="foo" checked> <input type="submit"> </form> If the checkbox is checked, $_POST gets...
1
by: ttamilvanan81 | last post by:
Hai i have using the checkbox in for loop. I need the urgent help from anyone, for example in the loop there is having 5 checkbox if i checked 3 of the ckeckboxes and 2 of the checkboxes are...
5
nev
by: nev | last post by:
i use mysql. i have a bound checkbox which returns 'checked' or 'unchecked' values into my database. what i want to achieve is for it to inject 1 or 0 into my database. how do?
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.