Jump to content

Posting selected records from html table...how?


dwest

Recommended Posts

Hi,
What is the best way to accomplish this?

I have an html table with 5 columns, the first of which contains a checkbox for each record, the rest containing data for each record.

I want to select specific records using the checkboxes and copy those records to a database table by posting the data to a processing page.

I understand that would require an SQL insert to get the data into the database.

My question then is how to code the gathering of the selected records and post them to the processing page?

I'd like to avoid Javascript if I can and keep it all PHP.

Thanks!
Wherever the form action is pointing to, on that page you would just grab the values of the checkboxes with $_POST.

[code]

$email = $_POST['email'];
$blah = $_POST['blah'];

mysql_query("INSERT INTO table_name (email, blah) VALUES ('$email', '$blah')")or die(mysql_error());

[/code]

Although you might have to get a little more complex with checkboxes and may want to store the values in an array. Also you need to find a way to check which ones they marked...which is where the array would come in handy.
The Checkbox would have the value of the record_id and the name of id[] which tells php that all the checked options go into the one array.

They would look like: <INPUT type='checkbox' name='id[]' value='1'> or whatever the value is.

That will then go to $_POST['id'] when the form is submitted and you can iterate through the array and move the records that were selected.

Cheers,
Dave
Sorry, I'm confused.

Example:

checkbox  |  Name  |  Address |  City
Checkbox    Ted      street1      New York
Checkbox    Bob      street2      Las Vegas
Checkbox    Will      street3      London

Say that Ted and Will are selected.

I need to copy the entire record for Ted and Will to another database.

So how do all the columns of data get captures for posting??

Thanks!
So each checkbox would be name="id[]" value="recordid"

Then in your processing script...
[code]
foreach($_POST['id'] as $selected){
  $db->query('INSERT INTO table SELECT * FROM oldtable WHERE id = $selected');
}
[/code]

Something like that.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.