Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

compare values of checkboxes 1

Status
Not open for further replies.

overflo

Technical User
Feb 10, 2003
70
AU
I have a form that has 4 checkboxes. They are displayed as checked or not as according to what is in the database.

When the form is submitted I need to compare the in initial state of checkboxes so that if any are changed then I can add the code to update database.

eg initial form

checkbox 1 - checked
checkbox 2
checkbox 3 - checked
checkbox 4

then edited to this

checkbox 1 - checked
checkbox 2
checkbox 3
checkbox 4- checked

The database entries would therefore not need to change for checkbox 1 or 2 but would need to update the database to reflect the changes of 3 & 4.

Is this possible or should I simply delete the all the entries when the form is submitted and add the new values??

Any ideas appreciated

Cheers
 
Two options:

The first is what you suggested: delete the entries and then re-add them as appropriate (this is my "personal" preferred method).

The other option you have, and it's a little more code intensive, is to create a duplicate set of fields and name them as old_checkbox_1_value, old_checkbox_2_value, etc. When the form is submitted, you compare the value of checkbox_1 to old_checkbox_1_value and see if they are the same or not. If not the same, update database; otherwise, leave the value alone.

Greg
 
I agree with 2nd option or should I say I would prefer 2nd option.

Now, there is a trick you can employ to simplify your PHP code. Use a name similar to an array variable so that you can reference the array once the form is submitted.

Code:
 <input name="newVal[]" type="checkbox" />
 <input name="newVal[]" type="checkbox" />
 <input name="newVal[]" type="checkbox" />
 <input name="newVal[]" type="checkbox" />
 <input name="newVal[]" type="checkbox" />

 <input name="oldVal[]" type="checkbox" value="<? echo $row['value']; ?>" />
 <input name="oldVal[]" type="checkbox" value="<? echo $row['value']; ?>" />
 <input name="oldVal[]" type="checkbox" value="<? echo $row['value']; ?>" />
 <input name="oldVal[]" type="checkbox" value="<? echo $row['value']; ?>" />
 <input name="oldVal[]" type="checkbox" value="<? echo $row['value']; ?>" />

Now, when the form is submitted
Code:
$newVal=$_POST['newVal'];
$oldVal=$_POST['oldVal'];
for($x=0; $x<sizeof($newVal); $x++) {
  if($newVal[$x] != $oldVal[$x]) {
     .... do your thing here ...
  }
}

Hope this points you in the right track!



--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Thankyou both. It does send me in the right direction.

Cheers
 
i'd add that not all browsers provide a value for checkboxes on submission. some simply provide a true or 1.

unclicked checkboxes are not submitted.

I suspect that the advice above intended that the old vals were stored in a hidden field or set thereof. i would counsel that it's not a good idea to rely on storing comparison values in hidden fields: they are very easily manipulated by a user, which would lose your data integrity.

Personally: i would go with the delete and rewrite option; and run both queries through a transaction. rolling back if there is a failure. that way your data integrity is maximised.

If you must go the second route then store the old values in a session key or, even better, don't store them. Look them up in the database. Or use Replace into syntax; or change your schema so that the checkbox values have unique IDs.
 
Thanks jpadie. Your advice is always excellent. (You've helped me a few times already on this project)

Cheers
 
I have to start thinking more along the lines of cross-browsers if I am to become a great web developer. I have been at this for a year now and every time I hear things like what jpadie mentioned

not all browsers provide a value for checkboxes on submission. some simply provide a true or 1.

AND

it's not a good idea to rely on storing comparison values in hidden fields: they are very easily manipulated by a user, which would lose your data integrity

All the development I've done where this kind of concerns would be valid have been done for private/custom applications where "I" held the power of choosing a browser and I, of course, chose FF.

That said, I am curious as to how could a user manipulate values of a hidden field?

Given jpadie points, I would have to retract my preference and suggest his. Funny thing is that I just finished a project where this very method is employed ... I guess back to the drawing board it is
--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
@southbeach

as an example of how hidden fields can be changed:

open up firefox and install firebug. then navigate to your page with hidden fields. open up firebug, select HTML and Edit, then change the hidden attribute to text. easy as that

that's the easy way, remember a get request or a post request is just text directed at a certain port. there is no reason why you can't create the request manually and pipe it through telnet.
 
jpadie,

A real eye opener and very valuable information for all of us. It sounds that hidden fields are best replaced for session variable when critical data is at stake.

A couple of days back I started to implement a captcha routine to fight robots - You just gave me a totally new enemy.

This is exactly what I mean by "learning by teaching/assisting".

Thanks!


--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top