Jump to content

[SOLVED] Checkbox PHP


TheFilmGod

Recommended Posts

I have a form. I got the mysql to work but I can't get one thing done: Checkboxes!!! ARG!!!

 

HTML Form Snippet

 

<p>
			Brunch
  		<input type="checkbox" name="activity[]" value="brunch" />
		<br />Dance
	<input type="checkbox" name="activity[]" value="dance" />
		<br />Game
  		<input type="checkbox" name="activity[]" value="game" />
</p>

 

// Call the checkbox inputs
		$activity = $_POST['activity'];

		// Define each checkbox its value
		$brunch = $activity[0];
		$dance = $activity[1];
		$game = $activity[2];

		echo "$brunch";
		echo "$dance";
		echo "$game";

		// if checkbox was checked it gives Y otherwise an N
		if ( $brunch == "brunch" ) {

			$brunch = "Y";
		}
		else {

			$brunch = "N";
		}

		if ( $dance == "dance" ) {

			$dance = "Y";
		}
		else {

			$dance = "N";
		}		

		if ( $game == "game" ) {

			$game = "Y";
		}
		else {

			$game = "N";
		}	

 

Half of the time it works. But sometimes it doesn't. "GAME" seems to be buggy. It doesn't work by itself!

Link to comment
Share on other sites

Try dumping the raw info using print_r($_POST).

 

EDIT: Ah, forgot! You can't store values in a checkbox. It's either on or off. One thing you could try is setting some different indexes for your control, like this:

<p>
<input type='checkbox' name='type[brunch]'>
<input type='checkbox' name='type[dance]'>
<input type='checkbox' name='type[game]'>
</p>
...
<?
//Check submit
$type=$_POST['type'];
foreach ($type as $key => $value) {
echo "$key: ".($value ? "Y" : "N")."<br>";
}
?>

Link to comment
Share on other sites

well when I echo the $brunch for example I get brunch. This only happens when its checked.

 

I just don't understand this array stuff! I simply want to know which values where checked. If checked they get a value of "Y" and an "N" if not. Please help! I"m on a time constraint!

Link to comment
Share on other sites

You can put checkboxes in a sub array of $_POST and you can define values for them.  However, the way you have your code setup, they won't necessarily be in the same order each time...from your snipped, if I check all three boxes, then they will be available at array index positions 0, 1, and 2.  However, if I chose only the last 2, then they will still be in 0 and 1, but not 2...you need to force them to be in the place you want...

 

Brunch
  		<input type="checkbox" name="activity[0]" value="brunch" />
		<br />Dance
	<input type="checkbox" name="activity[1]" value="dance" />
		<br />Game
  		<input type="checkbox" name="activity[2]" value="game" />

 

Remember that with checkboxes, if they box isn't checked, it's not included with the POST at all.

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.