Hello!

So here's what i did i fetch data from my database in a table each row has a checkbox on in. Now what i already did is when i press submit the data that will be shown in the next form is the row that has check on the checkbox

what i wanna try is a voting poll that uses checkbox so the user can vote multiple options

can you give me a simple example or explain the logic flow on how to do it?

thanks!

If you use checkboxes you can already select multiple options (they must have the same name and brackets next to them).

To find out which ones were selected, you loop through the array :3

Something like this...
The form:

What are your favorite types of pastry?  
<form method="post"> 
 <input type="checkbox" name="lol[]" value="cake"> Cake :D <br> 
 <input type="checkbox" name="lol[]" value="pie"> Pie <br> 
 <input type="checkbox" name="lol[]" value="cupcakes"> Cupcakes ^O^ <br> 
 <input type="checkbox" name="lol[]" value="brownies"> Brownies :D <br> 
 <input type="submit" value="Vote"> 
</form>

Then to retrieve the data in PHP:

You chose the following pastries as your favorites: 
<ul> 
 <?php 
  foreach ($_POST["lol"] as $pastry) { 
   if ($pastry=="cake") { //make sure the option they chose is part of the options :p 
    echo "<li>Cake</li>"; 
   } 
   if ($pastry=="pie") { 
    echo "<li>Pie</li>"; 
   } 
   if ($pastry=="cupcakes") { 
    echo "<li>Cupcakes</li>"; 
   } 
   if ($pastry=="brownies") { 
    echo "<li>Brownies</li>"; 
   } 
  } 
 ?> 
</ul>

how could i use the checkbox when the data will be comming from a database

Please post your table structure and the code with code tags that you are trying. This'll help us more to help you.

here's my index.php

<?php
 
    error_reporting(0);
    session_start();
	include ('conn.php');

$query = mysql_query("SELECT * from items");
echo "<table>";
echo "<tr>";
echo "<td>"."ITEM NAME"."</td>";
echo "<td>"."ITEM PRICE"."</td>";
echo "<form name='form1' action='submit_item.php' method='post'>";
echo "<td>"."<input type='submit' name='submit' value='Add Item'>"."</td>";
while($result = mysql_fetch_array($query))
{
  echo "<tr>";
  echo "<td>".$result['item_name']."</td>";
  echo "<td>".$result['price']."</td>";
  echo "<td>"."<input type='checkbox' name='$result[item_id]' value='$result[item_id]' id='$result[item_id]'>"."</td>";
}
echo "</form>";
echo "</table>";

?>
</body>
</html>

now in this submit_item.php the row that has been check will be shown here

<?php
 
    error_reporting(0);
    session_start();
	include ('conn.php');

$query = mysql_query("SELECT * from items");
echo "<table>";
echo "<tr>";
echo "<td>"."ITEM NAME"."</td>";
echo "<td>"."ITEM PRICE"."</td>";
echo "<form name='form1' action='submit_item.php' method='post'>";
echo "<td>"."<input type='submit' name='submit' value='Add Item'>"."</td>";
while($result = mysql_fetch_array($query))
{
  echo "<tr>";
  echo "<td>".$result['item_name']."</td>";
  echo "<td>".$result['price']."</td>";
  echo "<td>"."<input type='checkbox' name='$result[item_id]' value='$result[item_id]' id='$result[item_id]'>"."</td>";
}
echo "</form>";
echo "</table>";

?>
</body>
</html>

what i want to do is save the checked rows to another table in my database
thanks!

Member Avatar for diafol
"<td>"."<input type='checkbox' name='$result[item_id]' value='$result[item_id]' id='$result[item_id]'>"."</td>";

I don't think will work. Try:

"<td><input type=\"checkbox\" name=\"{$result['item_id']}\" value=\"{$result['item_id']}\" id=\"{$result['item_id']}\"></td>";

my code works fine it displays only the checked rows what i want to do is save them to another table

You have posted same code for both page.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.