Getting values from checkboxes selected in a table.

Posted 08 July 2010 - 06:45 PM

Hello everyone:

I am working with my last unit assignment, I have already found the way to include the checkboxes into each row of my table. I am still working with the php code to display the records selected in the table to be modified. I am not sure if I need to use the foreach() according to my code or if I must use any other way such using a button for each record instead checkboxes. I don't want to give up myself with this one so here is what I have until now:

I have include a picture of how it looks the webpage at the momment. I am sure this way will be much better to modify the existing records that one at a time. Please do not hesitate to ask for any other information, thanks in advance.

Replies To: Getting values from checkboxes selected in a table.

Re: Getting values from checkboxes selected in a table.

Posted 08 July 2010 - 06:50 PM

You need to have the checkboxes inside the form with the submit button.

The easiest way to do this, I'd say, would be to have each checkbox named something like 'user[#]' where # is the id of that entry in the database. You'd also want to change the action of the form to POST for this. Then, when you submit it, you'd simply look at the $_POST['user'] array for which ones have been selected. All you'd have to do is set each checkbox's value to something like 1. It really doesn't matter what value you set, because only the boxes you check are going to come up in the array.

Re: Getting values from checkboxes selected in a table.

Posted 08 July 2010 - 06:58 PM

Valek, on 08 July 2010 - 05:50 PM, said:

You need to have the checkboxes inside the form with the submit button.

The easiest way to do this, I'd say, would be to have each checkbox named something like 'user[#]' where # is the id of that entry in the database. You'd also want to change the action of the form to POST for this. Then, when you submit it, you'd simply look at the $_POST['user'] array for which ones have been selected. All you'd have to do is set each checkbox's value to something like 1. It really doesn't matter what value you set, because only the boxes you check are going to come up in the array.

I have tried that before, I mean when you say that I need "to have the checkboxes inside the form with the submit button" but do you know anyway I can do that without being printed in the screen as a new checkbox?

Re: Getting values from checkboxes selected in a table.

Posted 08 July 2010 - 07:03 PM

RGarcia, on 08 July 2010 - 07:58 PM, said:

do you know anyway I can do that without being printed in the screen as a new checkbox?

Not entirely sure what you mean there. I was merely suggesting putting the whole table inside the form the submit button is in. Then, when you submit it to whatever page you have handling the data, the checked checkboxes will show up in your $_POST array.

Otherwise, the submit button will submit for a form which has no elements. And your script will receive nothing.

Thanks to your recommendations I am very close to finaly show what I want. But I am still having problems to show all the selected rows, its only shows the last selected row and not showing the all. Here is the code for the php that will show the selected values, before was ModifySelected.php but I have changed to ShowSelected.php. After all records are changed, the user will be redirected to the principal php where the entire list of contacts is shown.

Anyone who have the knowledge can reply to this response, please provide useful information, thanks in advance.

Re: Getting values from checkboxes selected in a table.

I see $Modify getting constantly overwritten with the next one in line, until the loop finishes. Then, when you run your query, it only runs on the last one, because that is in fact the only query you're sending.

I see $Modify getting constantly overwritten with the next one in line, until the loop finishes. Then, when you run your query, it only runs on the last one, because that is in fact the only query you're sending.

For give me for inquirying in on this because this is all new to me..but if I understand you correctly " Valek" you are saying that this

while ($i < $size)
{
// Variable to hold the Phone Number value of each selected record.
$Modify = $_POST['selected'][$i];
++$i; <<< This should not be here..this is overwritting previous command???
}

Am I right on this..if not(sorry for intrusion of post)...oh well..this is from someone who is still in process of still learning..but you guys help on this alot..