tgavin Posted January 26, 2007 Share Posted January 26, 2007 I have a series of checkboxes (<input type="checkbox" name="foo[]">) that I need to get the names of and echo to the browser[code=php:0]foreach($_POST['foo'] as $foobar) { // do database stuff to get the checkboxes corresponding names and id nums $query = "select id,name from tbl where name = $foobar"; $sql = mysql_query($query,$conn) or die(mysql_error()); $row = mysql_fetch_assoc($sql); $id = $row['ln_id']; $name = $row['ln_name'];}echo $name1;echo $name2;etc...[/code]Not sure how to do that? Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 26, 2007 Share Posted January 26, 2007 What is the problem with it?Do print_r($_POST['foo']); and make sure they are set. Quote Link to comment Share on other sites More sharing options...
ballhogjoni Posted January 26, 2007 Share Posted January 26, 2007 Where is the $conn variable coming from? Quote Link to comment Share on other sites More sharing options...
cmgmyr Posted January 26, 2007 Share Posted January 26, 2007 This is another way to do it:[code=php:0]//find total records $query = "SELECT * FROM your_table"; $result = mysql_query($query) or die('Query failed: ' . mysql_error()); if (mysql_numrows($result) <= 0){ echo "No Information Available"; }else{ // Printing results in HTML $x = 0; while ($x < mysql_numrows($result)): $name = mysql_result($result, $x, 'name'); echo "<input name=\"foo[$x]\" type=\"checkbox\" id=\"foo[$x]\"/>$name<br />"; $x++; endwhile; }[/code] Quote Link to comment Share on other sites More sharing options...
tgavin Posted January 26, 2007 Author Share Posted January 26, 2007 Each checkbox in the form has a number as its value. So when the form is processed and run through the foreach() loop I'm pulling the corresponding name from mysql. I then want to take that name and echo it later in a success message.Everytime I run the loop, all that's retruned is the last pass of the loop. I'm just wondering how to capture that information from each iteration of the foreach() loop and (presumably) store it in an array for echoing later. Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted January 26, 2007 Share Posted January 26, 2007 i am thinking something like this:[code=php:0]$array = "";foreach($_POST['foo'] as $foobar) {// do database stuff to get the checkboxes corresponding names and id nums$query = "select id,name from tbl where name = $foobar"; $sql = mysql_query($query,$conn) or die(mysql_error());$row = mysql_fetch_assoc($sql);$array[] = array('id' => $row['ln_id'], 'name' => $row['ln_name']);}[/code]That stores the id's and names in an array which you can call on later.[code=php:0]foreach($array as $v){echo "ID is {$v[0]} and name is {$v[1]}<br>";}[/code]i think i might have mucked up my array in there somewhere but I think thats what your looking for. Quote Link to comment Share on other sites More sharing options...
tgavin Posted January 27, 2007 Author Share Posted January 27, 2007 I reworked it a little, but that pretty much had it. Thanks! :) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.