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

Unchecked checkbox - posts no data?

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
results, row by row, to the user. These results are returned in a form
(for editing) and then submitted back out again as an array (since the
field names are repeated), e.g :
<form type="text" name="subject[]">
<form type="checkbox" name="delete[]">
... plus many others.

My problem is that when the user makes the requested changes, and ticks
the 'delete' checkbox to X number of fields, those checkboxes which are
not checked post no data (is that correct?).

I verified this with "print_r($_POST)" (in PHP) and find that although
there is an array of all the data rows (subject[0] ; subject[1] etc...)
there only exists the 'delete' items which are checked, and needless to
say the array will then start from 0 and go up sequentially - so it
wouldn't match.

Hence regardless of which rows are marked for deletion, it's always the
first few which end up being deleted.

How (if possible) do I get the 'delete' values to correspond to the
correct position in the array? Or is there a better way of achieving this?

Many thanks,

Terence
Jul 23 '05 #1
3 3553
Terence Parker wrote:
I'm not sure whether this question demands a JavaScript
answer or a simple HTML one - so apologies for the
cross posting.
See the HTML specs.

<snip> My problem is that when the user makes the requested changes, and
ticks the 'delete' checkbox to X number of fields, those checkboxes
which are not checked post no data (is that correct?).
Correct, normal and expected. But unimportant as the absence of a
name/value pairs tells you that the corresponding checkbox was not
checked, and you know the total set of checkboxes you wrote into the
HTML so you can deduce the unchecked boxes by eliminating the values you
do get back.

<snip> How (if possible) do I get the 'delete' values to correspond
to the correct position in the array? Or is there a better
way of achieving this?


You give each checkbox a unique value attribute and use that value to
decide which rows to delete. (that would have been a server-side
scripting/CGI/PHP question).

Richard.
Jul 23 '05 #2
Fox


Terence Parker wrote:

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
results, row by row, to the user. These results are returned in a form
(for editing) and then submitted back out again as an array (since the
field names are repeated), e.g :

<form type="text" name="subject[]">
<form type="checkbox" name="delete[]">
... plus many others.


hmmm... shouldn't that be:

<input type="text" ... and <input type="checkbox" ?

anyway... your problem is that you need to index each [] -- otherwise,
when it arrives in $_POST [or whatever], PHP will index it
sequentially... 0, 1, 2, ...

to "autoindex":
for($indx = 0; $indx < $numlines; $indx++)
{

echo <<<html

<input type=text name=subject[$indx]>
<input type=checkbox name=delete[$indx]>
etc...
html;

}
when it goes to $_POST, you can

$toDelete = $_POST["delete"];

foreach($toDelete as $key => $value)
deleteLine($key);

this just goes straight through the array that $_POST receives ... if
you check out what's happening, only the indexes of the checked
checkboxes will be sent... [values here are not declared and will all
show "on" in this example, otherwise, you could set value=$indx (to use
the $value)]

so, if the user clicks the checkboxes to delete line #'s 3, 5, and 7,
the $_POST["delete"] values will only be:

[2] = "on"; // or whatever you set value= to in the input tag
[4] = "on";
[6] = "on";

(and this is ALL that is passed...)
(REMEMBER that arrays are zero-based -> 2 is the 3rd item, 4 is the 5th,
and so on... your line numbers should also begin with 0)

p.s. this is a server-side question - therefore, news:comp.lang.php
might be more appropriate.
Jul 23 '05 #3
Fox wrote:
p.s. this is a server-side question - therefore, news:comp.lang.php
might be more appropriate.

Thanks for your help (and to Richard too).

Now that both of you mention it: yes, it would be so much easier to do
in PHP to have the HTML output with the arrays indexed in the first
place rather than having 'delete[]'. So simple, and yet, for some
reason, that never crossed my mind.

It only required the addition of about five characters to my PHP code to
achieve that - how silly!!

Anyways, thanks for the tip - all works fine now.

- Terence
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

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...
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...
0
by: Jayender | last post by:
Hi , I have placed checkbox in my gridview .. now when i check the chekbox and click anybutton the checkbox becomes unchecked. but when i normally add checkbox in the page it doesnt happen but...
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...
1
vyon13
by: vyon13 | last post by:
this is the code that i have copy:i tried to add new function on it,... the function is if the checkbox is unchecked the value inputed by the user will be clear... how to do that.. thanks in...
6
by: beary | last post by:
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....
5
by: =?Utf-8?B?Y2hlY2tyYWlzZXJAY29tbXVuaXR5Lm5vc3BhbQ== | last post by:
I have a VS 2008 ASP.NET webform that has a reportview tag on it, accessing an .RLDC report in local report. The columns for the report are essentially: Month Item #1 Item#2 Item#3 ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.