Forum Moderators: coopster

Message Too Old, No Replies

Dynamic tables and checkboxes

         

ramoneguru

12:57 am on Apr 1, 2005 (gmt 0)

10+ Year Member



Well, here is what I am doing:
I am creating a table that will allow users to simply check a box and then have whatever they checked be displayed on another page...its not going as I planned. Here is what I have so far:

<html>
<body>
<?php

<form action = "phpForm3.php" value = "Submit" method = "POST">
<input type = "submit" value = "Go"/>

$result = mysql_query("SELECT * FROM Counties") or die(mysql_error());
if ($result)
{
print "<table width=200 border=1>\n";
print "<tr>\n";

print "<th>&nbsp; </th>\n";
print "<th> State </th>\n"; //2 fields in Counties table, State and County
print "<th> County </th>\n";

print "</tr>\n";
//create table
while ( $row = mysql_fetch_array($result) )
{
print "<tr>\n";

print "<td><input type=\"checkbox\" name=\"countyArray[]\" value=\"$row[CountyName]\"></td>\n";

echo "<td>{$row['State']}</td>\n";

echo "<td>{$row['CountyName']}</td>\n";

echo "</tr>\n";

}//end while
print "</table>\n";

}//end if
else
{
echo("<P>Error performing query: " .
mysql_error() . "</P>");

}

?>
</form>
</body>
</html>

Now this will create a nice looking table with checkboxes in the far left column. One problem, I cannot seem to associate the data (county, state) with the box that the user checked.
So, if they select the checkbox next to "CA Alemeda", then on the next page I want to display:
"You have selected: Alemeda, CA"
Any suggestions on how to do this?

iceman22

4:05 am on Apr 1, 2005 (gmt 0)

10+ Year Member



I've made a few changes to your code and your style, hope you don't mind. Basically you want to give the submit input a name so you can check if it is set, that it is clicked. It assigns each checkbox a unique name, County[number], there is a hidden input to let the form know when it's submitted how many checkboxes to check for.

<html>
<body>

<?php

if ( isset($_POST['submit']) ) { // if form is submitted, process it

for($i=1; $i<=$_POST['counties']; $i++) {
if ( isset($_POST["county$i"] ) ) {
print $_POST["county$i"]." is checked.<br/>";
}
}

} else { // if form isn't submitted, output the form

print "<form action=\"phpForm3.php\" method=\"POST\">\n";

$link = mysql_connect("", "", "");
mysql_select_db("");
$result = mysql_query("SELECT * FROM Counties") or die(mysql_error());
if ($result) {
print "<table width=200 border=1>\n";
print "<tr>\n";

print "<th>&nbsp; </th>\n";
print "<th> State </th>\n"; //2 fields in Counties table, State and County
print "<th> County </th>\n";

print "</tr>\n";
//create table
$i = 0;
while ( $row = mysql_fetch_array($result) ) {
$i++;
print "<tr>\n";

print "<td><input type=\"checkbox\" name=\"county$i\" value=\"$row[CountyName]\"></td>\n";

echo "<td>{$row['State']}</td>\n";

echo "<td>{$row['CountyName']}</td>\n";

echo "</tr>\n";

}//end while
print "</table>\n";
} else {
echo("<P>Error performing query: " .
mysql_error() . "</P>");

}
print "<input type=\"hidden\" name=\"counties\" value=\"$i\"/>\n";
print "<input type=\"submit\" name=\"submit\" value=\"Go\"/>\n";
}
?>
</form>
</body>
</html>

ramoneguru

6:41 am on Apr 1, 2005 (gmt 0)

10+ Year Member



Thanks, that worked like a charm. I never saw this used before:
"county$i", where you just cocatenate an index using that syntax. I am new to web development (I normally do C++ work and some C as well, hence my style.)

Its going to take some time to get used to this

(conditional) {
statements...
}//end conditional
else {
statements...
}

Thanks again, I'll just comment it "code which associates checkboxes to values courtesy of Iceman@webmasterworld.com" or something to that effect (or is it affect?)
--Nick

iceman22

6:59 am on Apr 1, 2005 (gmt 0)

10+ Year Member



You don't have to credit it heh, there could be a less dirty way of doing it, I thought of that way myself. I just tried emailing iceman22@webmasterworld.com, so we don't get automatic email address forwarding. In PHP you can concatenate strings just by playing a variable in the middle of text, as long as it's in double quotes. If your using single quotes it'll interpret it as it really is, with the dollar sign and all. To concatenate two variables outside of quotes, use a period $var1.$var2 for example.

ramoneguru

7:43 am on Apr 1, 2005 (gmt 0)

10+ Year Member



Cool, good to know for the future.
--Nick