Page 1 of 1

checkbox array from loop - SOLVED

Posted: Fri Dec 08, 2006 2:43 pm
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 />';
		}
	?>

Posted: Fri Dec 08, 2006 2:47 pm
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..

Posted: Fri Dec 08, 2006 2:53 pm
by psurrena
My idea was that when you retrieved the info again, the multiple submissions would be seperated by <li>

Posted: Fri Dec 08, 2006 3:00 pm
by feyd
You can add those on the processing page pretty easily with implode().

Posted: Fri Dec 08, 2006 3:12 pm
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....

Posted: Mon Dec 11, 2006 10:53 am
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!