[SOLVED] Dynamic Checkboxes
Posted: Mon Mar 05, 2007 12:56 pm
Well, at least I think they're dynamic. This is a relatively simple code, or it's supposed to be. But I'm having problems figuring out the last, and quite crucial, step.
It's a joint-email script that has several checkboxes, which can be added, edited and removed at the webmaster's choice. The checkboxes there I have no trouble with. The sign-up is what is giving me difficulties. It looks like this, so you know roughly what I'm doing:
And that makes the page. Works beautifully, and aligns the checkboxes in three columns. Wonderful!
The part that doesn't work is the page where it's submitted to. That code looks like this:
Now, I'm not 'quite' certain what the problem is. How it's set up is 3 separate databases: Users, which holds all user information, cBoxes, which holds the names of all checkboxes the webmaster deems needed, and interests, which holds only the email of the user, as well as the status of each checkbox. But instead of putting information into the checkboxes, it just puts a blank line in the email column in Interests. I can't think of another way to set up this script and I've been pulling my hair out over it, :p
The main problem, as far as I can tell, is that I don't have any of the checkbox names on-hand to use directly in the script. they're all located on the cBox sql database. Any ideas? I'd appreciate any and all help!
It's a joint-email script that has several checkboxes, which can be added, edited and removed at the webmaster's choice. The checkboxes there I have no trouble with. The sign-up is what is giving me difficulties. It looks like this, so you know roughly what I'm doing:
Code: Select all
<head>
<title>Joint Email Form</title>
</head>
<body>
<form action=fResults.php>
<table>
<br>
<tr>
<td width="71">First Name:</td>
<td width="306"><input type="text" size=50 name="fName" /></td>
</tr>
<br>
<tr>
<td>Last Name:</td>
<td><input type="text" size=50 name="lName" /></td>
</tr>
<br>
<tr>
<td>Address 1: </td>
<td><input type="text" SIZE=50 name="add1"></td>
</tr>
<br><br>
<tr>
<td>Address 2:</td>
<td><input type="text" SIZE=50 name="add2"></td>
</tr>
<br><br>
<tr>
<td>City:</td>
<td><input type="text" SIZE=50 name="city"></td>
</tr>
</table>
<table>
<tr>
<td width="71">State:</td>
<td width="24"><input type="text" SIZE=4 name="state"></td>
<td width="120"> </td>
<td width="53">Zipcode:</td>
<td width="60"><input type="text" SIZE=10 name="zip"></td>
</tr>
</table>
<table>
<tr>
<td width="71">Phone:</td>
<td><input type="text" SIZE=50 name="phone"></td>
</tr>
<br>
<tr>
<td width="71">Email:</td>
<td><input type="text" SIZE=50 name="email" /></td>
</table>
<?php
$dbh=mysql_connect (information) or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db (database);
$result = mysql_query("SELECT * FROM cBox", $dbh);
$rows = mysql_num_rows($result);
$i = 0;
$result = mysql_query("SELECT * FROM cBox", $dbh);
?>
<table>
<tr>
<?PHP
for($i=0;$i<$rows;$i++)
{
$r=mysql_fetch_array($result, MYSQL_ASSOC);
$name=$r["name"];
?>
<td width="150"><input type="checkbox" <?PHP echo "name='check [$i]'"; ?> value="true" /><?=$name?></td>
<?PHP
$x++;
if($x>2) {
$x=0;
echo "</tr><tr>";
}
}
?>
</tr>
</table>
<br /><br />
<input name="" type=submit value="Submit" />
</form>
</body>
</html>The part that doesn't work is the page where it's submitted to. That code looks like this:
Code: Select all
<head>
<title>Results</title>
</head>
<body>
<?PHP
$dbh=mysql_connect (information) or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db (database);
$sql = "INSERT INTO Users (fName, lName, add1, add2, city, state, zip, phone, email) VALUES ('$fName', '$lName', '$add1', '$add2', '$city', '$state', '$zip', '$phone', '$email')" or die(mysql_error());
$result = mysql_query($sql) or die(mysql_error());
$sql = "INSERT INTO interests (email) VALUES ('$email')";
$result = mysql_query($sql) or die (mysql_error());
$Result = mysql_query("SELECT * FROM cBox", $dbh);
$rows = mysql_num_rows($Result);
for($i=0;$i<$rows;$i++)
{
$r=mysql_fetch_array($Result, MYSQL_ASSOC);
$name=$r["name"];
$sql = "INSERT INTO interests ($name) VALUES ('$check[$i]')" or die(mysql_error());
$result = mysql_query($sql) or die(mysql_error());
}
echo "The data has been entered successfully. Please click <a href='http://www.homepage.com'>here</a> to go back to our homepage."
?>
</body>
</html>The main problem, as far as I can tell, is that I don't have any of the checkbox names on-hand to use directly in the script. they're all located on the cBox sql database. Any ideas? I'd appreciate any and all help!