0

hello! im trying to study how checkbox work, this is i want to do
i array a list of items (item name, price, etc) from my database i would like to put a checkbox beside each item, all the items that are checked will be posted on a next form

can you please show me a brief example of it thanks!

2
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by dudzkie
0

Try this script:
Imagine that your table has the following fields:
item_name, price, and item_id for the ID for each items.

<?php

    error_reporting(0);
    session_start();
//your db connection here.

// First, query all the items in your table.
$query = mysql_query("SELECT * from table_name");
echo "<table>";
echo "<tr>";
echo "<td>"."ITEM NAME"."</td>";
echo "<td>"."ITEM PRICE"."</td>";
echo "<form 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>";

Above code display all items in the table_name together with checkbox.
Now here's the code for submit_item.php where you can submit all checked items.

<?php
  error_reporting(0);
  session_start();
//your db connection here.

$query=mysql_query("select * from table_name");
while($result=mysql_fetch_array($query))
{
  $item=$result['item_id'];
  if(isset($_POST[$item])!=NULL)
  {
    $query1 = mysql_query("SELECT * FROM table_name where item_id='".$item."'");
    echo "<table>";
    echo "<tr>";
    echo "<td>"."ITEM NAME"."</td>";
    echo "<td>"."ITEM PRICE"."</td>";
    while($result = mysql_fetch_array($query))
    {
      echo "<tr>";
      echo "<td>".$result['item_name']."</td>";
      echo "<td>".$result['price']."</td>";
     }

        echo "</table>";
    						
  }				
			
}

?>
0

I got some error. Just change $query to $query1 on below query:

if(isset($_POST[$item])!=NULL)
  {
    $query1 = mysql_query("SELECT * FROM table_name where item_id='".$item."'");
    echo "<table>";
    echo "<tr>";
    echo "<td>"."ITEM NAME"."</td>";
    echo "<td>"."ITEM PRICE"."</td>";
    while($result = mysql_fetch_array($query)) //change $query into $query1
    {
      echo "<tr>";
      echo "<td>".$result['item_name']."</td>";
      echo "<td>".$result['price']."</td>";
     }
 
        echo "</table>";
 
  }
0

thanks a lot for the help! one more question why is ITEM NAME and ITEM PRICE keeps on repeating before an item
i try moving them above but it looks like that the item listed below don't belong on the table

thanks again

Edited by dudzkie: n/a

0

Try this.

$query=mysql_query("select * from table_name");
echo "<table>";
echo "<tr>";
echo "<td>"."ITEM NAME"."</td>";
echo "<td>"."ITEM PRICE"."</td>";
while($result=mysql_fetch_array($query))
{
  $item=$result['item_id'];
  if(isset($_POST[$item])!=NULL)
  {
    $query1 = mysql_query("SELECT * FROM table_name where item_id='".$item."'");
   
    while($result1 = mysql_fetch_array($query1))
    {
      echo "<tr>";
      echo "<td>".$result1['item_name']."</td>";
      echo "<td>".$result1['price']."</td>";
     }
 
        echo "</table>";
 
  }				
 
}
0

sorry but what you did is the same as what i did the table appears like this

ITEM NAME ITEM PRICE
pencil 10.00
notebook20.00pen15.00

seems like the other items don't belong on the table

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.