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
psurrena
Forum Contributor
Posts: 355 Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY
Post
by psurrena » Fri Dec 08, 2006 2:43 pm
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Dec 08, 2006 2:47 pm
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..
psurrena
Forum Contributor
Posts: 355 Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY
Post
by psurrena » Fri Dec 08, 2006 2:53 pm
My idea was that when you retrieved the info again, the multiple submissions would be seperated by <li>
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Dec 08, 2006 3:00 pm
You can add those on the processing page pretty easily with
implode() .
psurrena
Forum Contributor
Posts: 355 Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY
Post
by psurrena » Fri Dec 08, 2006 3:12 pm
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....
psurrena
Forum Contributor
Posts: 355 Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY
Post
by psurrena » Mon Dec 11, 2006 10:53 am
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!