Unchecked checkbox - posts no data?

T

Terence Parker

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
 
R

Richard Cornford

Terence said:
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.

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.

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.
 
F

Fox

Terence said:
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, might be more appropriate.
 
T

Terence Parker

Fox said:
p.s. this is a server-side question - therefore, 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top