[SOLVED] Dynamic Checkboxes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

kularian
Forum Newbie
Posts: 8
Joined: Mon Mar 05, 2007 12:38 pm

[SOLVED] Dynamic Checkboxes

Post by kularian »

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:

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

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>
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!
Last edited by kularian on Fri Mar 09, 2007 10:47 am, edited 1 time in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

It looks like you are relying on register_globals being on, which it shouldn't be. Have you tried referencing the superglobal $_POST array instead of just throwing a dollar sign in front of the form field names?
kularian
Forum Newbie
Posts: 8
Joined: Mon Mar 05, 2007 12:38 pm

Post by kularian »

Eh...nope, heh.

I'm still easing into PHP, so there are a few tricks and such that often escape me. I'll check into this $_POST idea and get back with you. Thanks!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

There is going to be more involved with this.

First, you are going to need to change the names of the checkbox elements (remiving the space). You are also going to need to fetch the count of the checkbox array when it comes in the $_POST array. Then you are going to have to loop through that array and use the checkbox array indeces to check isset() on each checkbox value passed, the use that index to tell you which one were checked.

Confused yet? :wink:
kularian
Forum Newbie
Posts: 8
Joined: Mon Mar 05, 2007 12:38 pm

Post by kularian »

Okay, I sort of understand the process. But I just can't wrap my head around the concept. I'm pretty good about analyzing stuff, so if you know a site off hand that has an example, if you could link me there, I should be able to figure it out. Or even so pseudo-code would work wonders. I just don't know the way to start setting it up is all. At least, I think that's my problem, :P

Thank you very much for your help so far though. You've shed light on a problem I thought was unsolvable.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Post some generated HTML (the part that has the checkboxes) so I can see if I can't throw out some psuedo code that can walk you through it with an example.
kularian
Forum Newbie
Posts: 8
Joined: Mon Mar 05, 2007 12:38 pm

Post by kularian »

Okay, hate to sound like a complete and utter newb to this...but what do you mean by generated HTML? Like...part of my above code itself...or the actual webpage where the checkboxes are or...*shrug*

I hate to waste your time like this, :P But I know very little about HTML, which often bites me when I'm programming, as PHP and HTML go hand in hand...but if you could specify I'd be more than willing to help, :oops:
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

Post by hrubos »

You will understand it quickly. Do small code and IF you have problem, so ask about it. I think you will be better

Have success !!!

I'm too newbie in PHP
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

kularian wrote:Okay, hate to sound like a complete and utter newb to this...but what do you mean by generated HTML? Like...part of my above code itself...or the actual webpage where the checkboxes are or...*shrug*
Run the code that makes the dynamic checkboxes, then do a view source and paste the section of the form with the checkbox HTML in it.
kularian
Forum Newbie
Posts: 8
Joined: Mon Mar 05, 2007 12:38 pm

Post by kularian »

Ah ha. Well, that was easy. :oops:

Code: Select all

<table>
<tr>

	<td width="150"><input type="checkbox" name='check [0]' value="true" />biking</td>

	
	<td width="150"><input type="checkbox" name='check [1]' value="true" />Boating</td>

	
	<td width="150"><input type="checkbox" name='check [2]' value="true" />Fishing</td>

	</tr><tr>
	<td width="150"><input type="checkbox" name='check [3]' value="true" />Hunting</td>

	</tr>
</table>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Ok, make sure to get rid of the space between the checkbox name and the opening array bracket...

Code: Select all

<table>
<tr>
   <td width="150"><input type="checkbox" name='check[0]' value="true" />biking</td>
   <td width="150"><input type="checkbox" name='check[1]' value="true" />Boating</td>
   <td width="150"><input type="checkbox" name='check[2]' value="true" />Fishing</td>
</tr>
<tr>
   <td width="150" colspan="3"><input type="checkbox" name='check[3]' value="true" />Hunting</td>
</tr>
</table>
Then, in your PHP code, you can do something like

Code: Select all

<?php
$count = count($checkboxarray); // This will be the same array used for the creation of the checkboxes
$checkboxes = $_POST['check'];

for ($i = 0; $i < $count; $i++)
{
  if (isset($checkboxes[$i]))
  {
    // A checkbox at index $i was set
  }
}
?>
Of course you are going to have to mess with this a little bit, but that is the concept. Build the checkbox list, then from that same dataset, loop that set checking if that index for the posted checkboxes is set. If it is, then it was checked. Else, it wasn't.
kularian
Forum Newbie
Posts: 8
Joined: Mon Mar 05, 2007 12:38 pm

Post by kularian »

Okay, the errors are gone, and it doesn't seem to want to input extra information into the email column of my database. Hurrah! Thank you!

However...it will not put information into the columns that refer to the different checkboxes. Not sure how to go about doing that, though. The main problem is that the names of the different items aren't fixed; that is, they can be changed and edited. Right now, I can draw out their names by doing:

$r=mysql_fetch_array($Result, MYSQL_ASSOC);
$name=$r["name"];

But when I try to put it in the 'interests' database, I tried doing this:

$sql = "INSERT INTO interests ('$name') VALUES ('$checkboxes[$i]')" or die(mysql_error());
$result = mysql_query($sql) or die(mysql_error());

However, it doesn't want to input the data. I'm not really sure how to go about doing this...anyone have any ideas?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

When you say it doesn't want to input the data, are you getting an error message, or just not seeing the data?
kularian
Forum Newbie
Posts: 8
Joined: Mon Mar 05, 2007 12:38 pm

Post by kularian »

Sorry. No error message; it goes through just fine.

But when I check the databases, there's no information input for any of the checkboxes, even if I check them. I've tried checking them all to see if that might (somehow) be the issue, but no dice there either. Should I be trying to go about this a different way?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Two things to recommend...

1. Do a var_dump($_POST) immediately after whatever check you do for if the form was posted.
2. Echo out the SQL to see what the database server is seeing. Then try to hit the DB with that hard coded SQL to see if it works at the DB level.
Post Reply