problem with populating check input with posted data

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

Post Reply
horseatingweeds
Forum Newbie
Posts: 6
Joined: Mon Aug 13, 2007 2:57 am

problem with populating check input with posted data

Post by horseatingweeds »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


This function I wrote works fine for all but the FIRST input for some reason. It simple keeps the users inputs in a group of check elements but doesn't work for the first element in the group.

Anyone see the problem or have a better way of doing this?

here is the code:

Code: Select all

$pup = '';
$adult = '';
$trained = '';
$stud = '';
$rescue = '';
Some other code including the include for the form below.

Code: Select all

function popActivities($a)
{	
if (isset($_POST['chkActivities']))
{	
	foreach($_POST['chkActivities'] AS $activity)
	{	
		if ($activity == 'Doberman Puppies')
		{
		GLOBAL $pup;
		$pup = "checked='checked'";
		}
		if ($activity == 'Adult Dobermans')
		{
		GLOBAL $adult;
		$adult = "checked='checked'";
		}
		if ($activity == 'Trained Dobermans')
		{
		GLOBAL $trained;
		$trained = "checked='checked'";
		}
		if ($activity == 'Stud Service')
		{
		GLOBAL $stud;
		$stud = "checked='checked'";
		}
		if ($activity == 'Doberman Rescue')
		{
		GLOBAL $rescue;
		$rescue = "checked='checked'";
		}
	}
}

echo $a;
}

Code: Select all

<fieldset>
<legend>What do Your Activities Provide?</legend>
<input type='checkbox' name='chkActivities[]' value='Doberman Puppies'
<?php popActivities($pup); ?>  /><label>Doberman Puppies</label><br />

<input type='checkbox' name='chkActivities[]' value='Adult Dobermans'
<?php popActivities($adult); ?> /><label>Adult Dobermans</label><br />

<input type='checkbox' name='chkActivities[]' value='Trained Dobermans'
<?php popActivities($trained); ?> /><label>Trained Dobermans</label><br />

<input type='checkbox' name='chkActivities[]' value='Stud Service'
<?php popActivities($stud); ?> /><label>Stud Service</label><br />

<input type='checkbox' name='chkActivities[]' value='Doberman Rescue'
<?php popActivities($rescue); ?> /><label>Doberman Rescue</label><br />
</fieldset>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

Why don't you just use a form and get the input from there?

Also, you could use a switch statement inside your function as opposed to if...
horseatingweeds
Forum Newbie
Posts: 6
Joined: Mon Aug 13, 2007 2:57 am

Post by horseatingweeds »

Hi aceconcepts,

I'm using posted because it doesn't need an extra form and I plan to interact with a DB anyway.

Ive replaced the ifs with a switch, but I'm still having the same problem. It works for all the check inputs in the group except for the first one.

Where is this problem coming from? This code:

Code: Select all

foreach($_POST['chkActivities'] AS $activity)
When echoed produces this:

Doberman Puppies
Adult Dobermans
Trained Dobermans
Stud Service
Doberman Rescue

So I don't see why the following misses the first check element:

Code: Select all

function popActivities($a)
{	
if (isset($_POST['chkActivities']))
{	
	foreach($_POST['chkActivities'] AS $activity)
	{	
		switch ($activity)
		{
		case 'Doberman Puppies':
			GLOBAL $pup;
			$pup = "checked='checked'";
			break;
		case 'Adult Dobermans':
			GLOBAL $adult;
			$adult = "checked='checked'";
			break;
		case 'Trained Dobermans':
			GLOBAL $trained;
			$trained = "checked='checked'";
			break;
		case 'Stud Service':
			GLOBAL $stud;
			$stud = "checked='checked'";
			break;
		case 'Doberman Rescue':
			GLOBAL $rescue;
			$rescue = "checked='checked'";
			break;
		}
	}
}

echo $a;
}
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

Could you post the output of:

Code: Select all

print_r($_POST['chkActivities'])
in both cases - checked and unchecked.
There are 10 types of people in this world, those who understand binary and those who don't
horseatingweeds
Forum Newbie
Posts: 6
Joined: Mon Aug 13, 2007 2:57 am

Post by horseatingweeds »

Hi VladSon,

With the first input checked:

Array ( [0] => Doberman Puppies )

without it checked I jsut get a notice of an undefined index.
horseatingweeds
Forum Newbie
Posts: 6
Joined: Mon Aug 13, 2007 2:57 am

Post by horseatingweeds »

Here, I've made a page consisting only of the problemed code:

Code: Select all

<?php
$pup = "";
$adult = "";
$trained = "";
$stud = "";
$rescue = "";
function popActivities($a)
{	
if (isset($_POST['chkActivities']))
{	
	foreach($_POST['chkActivities'] AS $activity)
	{	
		switch ($activity)
		{
		case 'Doberman Puppies':
			GLOBAL $pup;
			$pup = "checked='checked'";
			break;
		case 'Adult Dobermans':
			GLOBAL $adult;
			$adult = "checked='checked'";
			break;
		case 'Trained Dobermans':
			GLOBAL $trained;
			$trained = "checked='checked'";
			break;
		case 'Stud Service':
			GLOBAL $stud;
			$stud = "checked='checked'";
			break;
		case 'Doberman Rescue':
			GLOBAL $rescue;
			$rescue = "checked='checked'";
			break;		
		}
	}
}

echo $a;
}
if (isset($_POST['chkActivities']))
{foreach($_POST['chkActivities'] AS $activity) echo $activity . '<br />';}
?>
<form name='form1' id='form1' enctype='multipart/form-data'	
action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "post" >

<input type='checkbox' name='chkActivities[]' value='Doberman Puppies'
<?php popActivities($pup); ?>  /><label>Doberman Puppies</label><br />

<input type='checkbox' name='chkActivities[]' value='Adult Dobermans'
<?php popActivities($adult); ?> /><label>Adult Dobermans</label><br />

<input type='checkbox' name='chkActivities[]' value='Trained Dobermans'
<?php popActivities($trained); ?> /><label>Trained Dobermans</label><br />

<input type='checkbox' name='chkActivities[]' value='Stud Service'
<?php popActivities($stud); ?> /><label>Stud Service</label><br />

<input type='checkbox' name='chkActivities[]' value='Doberman Rescue'
<?php popActivities($rescue); ?> /><label>Doberman Rescue</label><br />
<input type='submit' name='submit' value='Next ->' />
</form>
<p>print_r($_POST['chkActivities']) produces this:</p>
<?php
print_r($_POST['chkActivities'])
?>
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

Not entirely sure why the first form input element doesn't validate.

If you remove the the first input element (Doberman Puppies) you will notice that "Adult Dobermans" will not be checked. It seems as though the ver first array value is being ignored.

I played about with it and i know its not ideal but the following works:

Edit the input element:

Code: Select all

<input type='checkbox' name='chkActivities[]' value='Doberman Puppies'
<?php popActivities($pup); if(isset($pup)) echo"checked=\"checked\""; ?> /><label>Doberman Puppies</label><br />
horseatingweeds
Forum Newbie
Posts: 6
Joined: Mon Aug 13, 2007 2:57 am

Post by horseatingweeds »

Yeah, I'm starting to suspect a bug. I've changed it to a simpler version that should do the trick now.
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Post by novice4eva »

Hi there horseatingweeds,
If you go through the flow of the program one by one, you will figure out the problem that lies....well if you study your code you'll see that after you've submitted the form the first call to "popActivities($pup)" sets the variables

$pup;
$adult;
$trained;
$stud;
$rescue;

DID U GET THE PROB NOW?? NOO! :!:
Well then, back to first call to the popActivities($pup) after the form submission, here $pup is set to nothing...it is after this first call your global variables are set(all of them in the first call).....ie for those checkboxes that are set the value will be set to "checked='checked' " for corresponding variables, hence on your consecutive calls to popActivities() your variables are set so it prints the desired checked effect....

ummm i don;t think i made things clearer so i'll give the solution then

Code: Select all

<?php
$pup = "";
$adult = "";
$trained = "";
$stud = "";
$rescue = "";
if (isset($_POST['chkActivities']))
{
	popActivities(NULL);       
}

function popActivities($a)
{       
if (isset($_POST['chkActivities']))
{       
        foreach($_POST['chkActivities'] as $activity)
        {       
                switch ($activity)
                {
					case 'Doberman Puppies':
							GLOBAL $pup;
							$pup = "checked='checked'";
							break;
					case 'Adult Dobermans':
							GLOBAL $adult;
							$adult = "checked='checked'";
							break;
					case 'Trained Dobermans':
							GLOBAL $trained;
							$trained = "checked='checked'";
							break;
					case 'Stud Service':
							GLOBAL $stud;
							$stud = "checked='checked'";
							break;
					case 'Doberman Rescue':
							GLOBAL $rescue;
							$rescue = "checked='checked'";
							break;   
                }
        }
}

echo $a;
}

?>
<form name='form1' id='form1' enctype='multipart/form-data'     
action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "post" >

<input type='checkbox' name='chkActivities[]' value='Doberman Puppies'
<?php popActivities($pup); ?>  /><label>Doberman Puppies</label><br />

<input type='checkbox' name='chkActivities[]' value='Adult Dobermans'
<?php popActivities($adult); ?> /><label>Adult Dobermans</label><br />

<input type='checkbox' name='chkActivities[]' value='Trained Dobermans'
<?php popActivities($trained); ?> /><label>Trained Dobermans</label><br />

<input type='checkbox' name='chkActivities[]' value='Stud Service'
<?php popActivities($stud); ?> /><label>Stud Service</label><br />

<input type='checkbox' name='chkActivities[]' value='Doberman Rescue'
<?php popActivities($rescue); ?> /><label>Doberman Rescue</label><br />
<input type='submit' name='submit' value='Next ->' />
</form>
<p>print_r($_POST['chkActivities']) produces this:</p>
<?php
print_r($_POST['chkActivities'])
?>
Hope it shed some light :wink:
Post Reply