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

using onchange to retrieve a checkbox value.

P: n/a
Daz
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.

Oct 8 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Oct 8 '06 #2

P: n/a
Daz

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.
Hello again Jerry. Thanks for the reply. The form is submited each time
that a checkbox is selcted. It's basically a list of books (if you can
remember the MySQL problem I was discussing in a different group. I
have named all of the boxes the same as I am only interested in the
single value from the box that was clicked on (whether it was to check
or uncheck the box).

I had an idea, but I am not sure what implications it might have on
users with low speed connections. I could have a list of 'upto' 100
hidden <inputelements, containing the values of the checkboxes when
the page was generated. I can iterate through these and then see which
box was changed by matching the name of the hidden element with the ID
of the box on the page. This would save comparing each item to that in
the database to see whether or not it exists, or having the need to
update/validate upto 100 user rows in the database. The list is much
like Google, and shows results in the following numbers
10,20,30,50,100. Once again, I don't know how quickly 100 $_POST
variables will be uploaded from the user, I can't imagine it would be
THAT slow, but I wouldn't mind a second opinion, first.

I just don't understand why unchecking a box posts the value of the
last box that was checked...

Cheers!

Daz.

Oct 8 '06 #3

P: n/a
Daz

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?

Oct 8 '06 #4

P: n/a
Daz

Daz wrote:
I just don't understand why unchecking a box posts the value of the
last box that was checked...
Scrap that. I have just understood what you were saying about how POST
works.

Oct 8 '06 #5

P: n/a
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.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Oct 8 '06 #6

P: n/a
Daz

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.

Oct 8 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.