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
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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>";
   }
.....

Link to comment
Share on other sites

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;
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.