Jump to content

Multiple checkboxes in a while


DenchyKins

Recommended Posts

Ok, so the idea here is that the code displays all of the gravestones in the table (which it does), and a checkbox is created for every item so that this checkbox can be clicked if a person can supply it.

The problem i have is giving each checkbox a different name in the while, i tried $row['GravestoneID'] but it didn't like this, i need a way to check if each one has been checked individually to then change a boolean in a different table. Any pointers would be good, also if you think im going about this in completely the wrong way, please tell me, this is the first idea i had, thanks:

 

<?php

   $connection = mysql_connect("****", "****", "****")
      or die('Could not connect: ' . mysql_error());
   mysql_select_db("COMP10900_M3", $connection)
   or die('Could not select database');

   $result = mysql_query("SELECT * FROM gravestone_table");

echo "<form>";

   while($row = mysql_fetch_array($result))
   {
     echo "<dl>";
     echo "<dt>" .$row['GravestoneName'] . "</dt>";
     echo "<dd>" .$row['GravestoneDescription'] . "</dd>";
     echo "</dl>";
     echo "<input type = 'checkbox'>" . "We can supply this";
     echo "<hr>";
   }

echo "</form>";

   ?>

Link to comment
https://forums.phpfreaks.com/topic/94558-multiple-checkboxes-in-a-while/
Share on other sites

I would use

 

    echo "<input type = 'checkbox' name="checkbox[]" value="The value you want e.g. ID">" . "We can supply this";

 

That will create an array called $_POST['checkbox'] then use a foreach to loop through the data

 

foreach ($_POST['checkbox'] as $ID) {
rcho $ID;
}

 

 

Regards

Liam

Oooh the array is a good idea, however my main problem is that for my value, i cannot set it to $row['GravestoneID'] as PHP doesn't like this, i have no idea how to make it have a unique value for each time the while happens, does that make sense? any ideas?

Oooh the array is a good idea, however my main problem is that for my value, i cannot set it to $row['GravestoneID'] as PHP doesn't like this, i have no idea how to make it have a unique value for each time the while happens, does that make sense? any ideas?

 

What doesn't PHP 'like' about it? It should be an integer, and printed like so:

 

.....
while($row = mysql_fetch_array($result))
   {
     echo "<dl>";
     echo "<dt>" .$row['GravestoneName'] . "</dt>";
     echo "<dd>" .$row['GravestoneDescription'] . "</dd>";
     echo "</dl>";
     echo "<input type=\"checkbox\" name=\"checkbox[]\" value=\"".htmlspecialchars($row['GravestoneId'])."\"> We can supply this";
     echo "<hr>";
   }
.....

well my gravestone IDs are GS01, GS02, GS03 etc because of the need for this in another table, i assume you meant these need to be integers, and therefore i have a big problem :/.

 

also, what would i need to place in *HERE*

<?php

foreach ($_POST['checkbox'] as *HERE*)
{
echo $ID;
}

?>

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.