Jump to content

[SOLVED] Checkbox


anser316

Recommended Posts

Hi I have not solved this checkbox problem for ages! so i would really appreciate if i could get some help.

 

From a table on mysql, i have listed specific rows. There are two PK's, (DRUG_ID,BRANCH_ID)

I want to select certain rows, and display them on another form.

e.g.

DID BID  STOCK

1  1    3

1  2    4

2  1    6

2  2    5

 

I have tried a few different ways and none have worked. This is what i currently have

 

Form 1

$result2 =mysql_query("SELECT ....");
echo "<form action= conorder.php method=POST>";
  echo "<table border='1'>";
   echo "<tr> <th>DRUG ID</th> <th>Branch ID</th><th>STOCK</th></tr>";
   $counter=0;
   while($row = mysql_fetch_array( $result2 )) {
   echo "<tr><td>";
   echo "<input type='hidden' name=drug_id[$counter] value='$row[drug_id]'>";
   echo $row['drug_id'];
   echo "</td><td>";
   echo "<input type='hidden' name=branch_id[$counter] value='$row[branch_id]'>";
   echo $row['branch_id'];
   echo "</td><td>";
   echo "<input type='hidden' name=total_stock[$counter] value='$row[total_stock]'>";
   echo $row['total_stock'];
   echo "</td><td>";
   echo "<input type=checkbox name=ticked[] value='$counter'>";
   echo "</td></tr>"; 
   $counter++;
   }
  echo "</table>";
echo "<input type =submit value= Submit>";
echo "<input type = reset>";
echo "</form>";

 

Form 2

if (isset($_POST['ticked'])) {
  for ($i=0; $i<count($_POST['ticked']); $i++) {
   echo "<br>drug id: ";
   echo $_POST[drug_id][$i];
   echo "    branch id: ";
   echo $_POST[branch_id][$i];
   echo "    stock: ";
   echo $_POST[total_stock][$i];}
}

 

The problem I am getting is as follows:

If for example i select rows 2 and 3, I get rows 1 and 2.

Or if i select row 4, i get row 1.

 

Where have i gone wrong?

Link to comment
https://forums.phpfreaks.com/topic/100857-solved-checkbox/
Share on other sites

When you use <input type=checkbox name=ticked[] value='$counter'> it this tells the PHP interpreter to add an element to the ticked array.

 

So if you do

ticked[] = 4

 

you get

Array(
0 => 4
)

 

If you do

 

ticked[] = 2
ticked[] = 3

 

You get:

Array(
0 => 2,
1 => 3
)

 

So try this:

 

if (isset($_POST['ticked'])) {
  for ($i=0; $i<count($_POST['ticked']); $i++) {
   $row_value = $_POST['ticked'][$i];
   echo "<br>drug id: ";
   echo $_POST['drug_id'][$row_value];
   echo "    branch id: ";
   echo $_POST['branch_id'][$row_value];
   echo "    stock: ";
   echo $_POST['total_stock'][$row_value];}
}

 

 

Link to comment
https://forums.phpfreaks.com/topic/100857-solved-checkbox/#findComment-515823
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.