You are here: Re: using onchange to retrieve a checkbox value. « PHP Programming Language « IT news, forums, messages
Re: using onchange to retrieve a checkbox value.

Posted by Daz on 10/08/06 19:29

Jerry Stuckle wrote:
> Daz wrote:
> > Jerry Stuckle wrote:
> >
> >>Daz wrote:
> >>
> >>>Hi everyone.
> >>>
> >>>Firstly, I apologise if this i not what you would call a PHP problem. I
> >>>get quite confused as to what lives in which realm, so if this
> >>>shouldn't be posted here, please suggest where I should post it.
> >>>
> >>>I have created a form, which consists of a list of items, each with a
> >>>checkbox. When a checkbox is checked or unchecked, the page should be
> >>>refreshed. During the refresh, the data is validated and the MySQL
> >>>database is updated etc...
> >>>
> >>>The problem I am having is using onchange (which I believe is
> >>>javascript). When I check a box, everything works great, the database
> >>>updates as it should, the page refreshes and reflects the change.
> >>>However, when I 'uncheck' the box. for some reason, it posts the value
> >>>of the last checkbox that was checked, and unchecks that... I am trying
> >>>to find a pure PHP method (even though I accept that I will need to
> >>>learn Javascript sooner or later), which will help me find the one box
> >>>that was unchecked, (as it's almost impossible to check more at any one
> >>>time as the page refreshes immediately).
> >>>
> >>>The list of items can be filtered, and there are just under 3600 items
> >>>in total (hence why they are filtered). Each items has a uniique id, so
> >>>to save iterating through every possible checkbox, and cross
> >>>referencing each one with the database to see which one has changed, I
> >>>named each checkbox 'check' and the value of each one contains the 'id'
> >>>of the item in the list.
> >>>
> >>>As far as I know, this should work fine, as it should submit the value
> >>>of the checkbox that was clicked when it's clicked upon, but it's not
> >>>working for unclicked checkboxes.
> >>>
> >>>Any input would be appreciated.
> >>>
> >>
> >>If your onchange event is submitting the form, only the values of the
> >>checked boxes are sent in the form. If a box is not checked, it's value
> >>isn't sent. And the you're doing it (naming each box "check") means you
> >>will only get one value - if multiple values are sent, all but the last
> >>will be overwritten by subsequent checked boxes.
> >>
> >>Also, these boxes may not be in the order you check them - the browser
> >>is free to send them in any order, although it is generally the order
> >>found on the page.
> >>
> >>There is no good PHP solution to this other than getting the entire form
> >>and checking against the database. However, Ajax can give you
> >>additional options, although it will require learning javascript.
> >>
> >
> > I was also wondering as to whether it would make any different if I
> > changed each box to use onchange instead of the actual form, or perhaps
> > have each box as a separate form?
> >
>
> Yes, you could have each box in a separate form. You could also call
> each box something like 'check[]'. Then you will get an array of all
> checked boxes. Of course, you'll have to determine which one has
> changed by comparing against the database, or values you've saved.
>
> And you could save them in the form as hidden fields, in a session (as
> long as you don't get too many), etc. Lots of ways.
>
Fantastic! Thanks Jerry.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация