473,372 Members | 1,312 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,372 software developers and data experts.

using onchange to retrieve a checkbox value.

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
6 4300
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Paul Eghbal | last post by:
Hi all, I'm trying to use the following script: <script language="javaScript"> function setrepto(){ document.aForm.repno.value = document.aForm.rep.options.value; } </script>
4
by: Jack | last post by:
Hi, I have a checkbox the value which goes to a database via a asp page that builds the sql string. In the front end asp page, the checkbox code is written as follows: <i><input...
3
by: Amy Snyder | last post by:
For what I thought would be a very simple task is turning out to be very frustrating. I have been trying for a day now to extract checkbox values from my form and nothing is working. On my form...
0
by: Mirovk | last post by:
I am trying to take actions using onchange with an option list without success. I am not able to fully populate the list without a bad bad behavior (the list is not getting populated) What I...
4
by: Matt | last post by:
I am no JavaScript guru so please bear with me and be as detailed as possible with your response. I thank you in advance. I have an ASP page that contains form elements. I also have an inline...
21
by: giandeo | last post by:
Hello Experts. Is it possible to retrieve the value from a populated pull down menu from a database and then use that value to access the same database to get the related fields. Example: ...
2
by: palanidharma | last post by:
hi, i writing the code for selectd check box value deleted.but the code not working <?php include("conn.php"); $sel= mysql_select_db("sangoma"); //include("function.php"); ?>
9
by: magix | last post by:
If I have <form action="process.asp" method="get" name="form1"> .... .... <input TYPE=checkbox name=sports VALUE=Cricket> <input TYPE=checkbox name=sports VALUE=Swimming> <input...
1
by: Greg Eyres | last post by:
Apologies if this is a stupid question ... I'm new to this PHP world! I have got an html form that has checkboxes dynamically created from MySQL. I use an array to create the list of checkboxes. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.