checkbox array from loop - SOLVED

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

checkbox array from loop - SOLVED

Post by psurrena »

Hello,

Here's what the setup is: I have a list of items pulled from a table with a checkbox next to them. The user selects multiple checkboxes, clicks submit and the values are submitted to a different table. Simple enough but when I check multiple boxes, the only value submitted is the last checkbox. Should I put all the checkbox values into an array and submit that? If so, some guidance please.

[Please note the code is at the bare minimum now to make it easier for me to debug]

Thanks,
Pete

Code: Select all

<?php
	if(isset($_POST['add'])) {

		$fintext = $_POST['finance'];
		
		$query = "INSERT INTO fin_acc_final (fin_text) VALUES ('$fintext')";
	
		mysql_query($query) or die (mysql_error());
	
	}
?>

Code: Select all

<?php
		$query = ('SELECT * FROM fin_acc ORDER BY id ASC');
		$result = mysql_query($query);
		mysql_query($query) or die (mysql_error() . 'Cannot Retrieve Information');

		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
			$id = $row['id'];
			$fintext = $row['fin_text'];
	
			echo '<input type="checkbox" name="finance" value="<li>' . $fintext .'</li>">' . $fintext . '<br />';
		}
	?>
Last edited by psurrena on Mon Dec 11, 2006 10:54 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

echo '<input type="checkbox" name="finance[]" value="' . $id . '" />' . $fintext . '<br />';
I'm not sure why you have HTML in the value attributes..
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Post by psurrena »

My idea was that when you retrieved the info again, the multiple submissions would be seperated by <li>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You can add those on the processing page pretty easily with implode().
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Post by psurrena »

Code: Select all

<?php

$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);

echo $comma_separated; // lastname,email,phone

?>
That would clean things up a bit.

Code: Select all

echo '<input type="checkbox" name="finance[]" value="$fintext">' . $fintext . '<br />';
just inserts the value "array" into the table when using the same $POST info as before....
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Post by psurrena »

So the finance arrary is created in a while loop by 'name="finance[]"' as suggested by Feyd:

Code: Select all

echo '<input type="checkbox" name="finance[]" value="'. $fintext .'" />' . $fintext . '<br />';
Then the implode funtion is used after the form is submitted to define the array and insert the html tags (as suggested).

Here is the simplified code just to display the results:

Code: Select all

$finance = '<li>' . implode('<li>' , $_POST['finance']);

//just for demo purposes	
echo "<ul>";
echo $finance;
echo "</ul>";
Thanks for everyones help!
Post Reply