checking if checkboxes are checked

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

jmilane
Forum Commoner
Posts: 89
Joined: Mon Aug 07, 2006 6:05 pm

checking if checkboxes are checked

Post by jmilane »

i have a few checkboxes. i want the user to have to pick at least one of them.

this is my code

Code: Select all

$checked = "no";


if ( $employmentprog <>'') 
{
$checked = "yes";
}


if ($businessprog <>'') 
{
$checked = 1;
}

if ($certprog <>'') 
{
$checked = 1;
}

if ($youthprog <>'') 
{
$checked = 1;
}

if ($parentprog <>'') 
{
$checked = 1;
}

if ($policyprog <>'') 
{
$checked = 1;
}

if ($checked = 'no') 
{
echo "Please use your browser to go back and select at least one program.";
}
Doesnt work.

Help?

Thank you!
User avatar
emmbec
Forum Contributor
Posts: 112
Joined: Thu Sep 21, 2006 12:19 pm
Location: Queretaro, Mexico

Post by emmbec »

have you tried using JavaScript to do this before sending the form???

something like this:

Code: Select all

<SCRIPT LANGUAGE="JavaScript">
function validate(form) {
	if (form.my_checkbox.checked==false){
		alert("Select a checkbox before continuing");
		return (false);
	}
 }
</script>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Javascript is not a solution for this. It may be used, but it cannot be the solution.

If the checkbox field is in the submission data it was checked. If it is not in the submission data, you can guess what this means.
jmilane
Forum Commoner
Posts: 89
Joined: Mon Aug 07, 2006 6:05 pm

Post by jmilane »

emmbec wrote:have you tried using JavaScript to do this before sending the form???

something like this:

Code: Select all

<SCRIPT LANGUAGE="JavaScript">
function validate(form) {
	if (form.my_checkbox.checked==false){
		alert("Select a checkbox before continuing");
		return (false);
	}
 }
</script>
it is a series of checkboxes and I dont know javascript well at all...

id like to do it in php...

thanks, though!
jmilane
Forum Commoner
Posts: 89
Joined: Mon Aug 07, 2006 6:05 pm

Post by jmilane »

feyd wrote:Javascript is not a solution for this. It may be used, but it cannot be the solution.

If the checkbox field is in the submission data it was checked. If it is not in the submission data, you can guess what this means.
So if it was not checked, it wont even be in $_POST?

Interesting, but then my code should still work... right?
Last edited by jmilane on Thu Sep 21, 2006 4:15 pm, edited 2 times in total.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

correct
jmilane
Forum Commoner
Posts: 89
Joined: Mon Aug 07, 2006 6:05 pm

Post by jmilane »

The Ninja Space Goat wrote:correct
if ( isset($employmentprog)) {
$checked = "yes";
}

something like this, then?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

jmilane wrote:So if it was not checked, it wont even be in $_POST?

Interesting, but then my code should still work... right?
Technically, your Javascript has potential errors. But this is a discussion outside the scope of this thread.
jmilane
Forum Commoner
Posts: 89
Joined: Mon Aug 07, 2006 6:05 pm

Post by jmilane »

aaah!!!

T_PAAMAYIM_NEKUDOTAYIM ?????
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Translates to "double colon" ..
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I responded "correct" to the part that said "So if it was not checked, it wont even be in $_POST? " (just to clarify)
jmilane
Forum Commoner
Posts: 89
Joined: Mon Aug 07, 2006 6:05 pm

Post by jmilane »

The Ninja Space Goat wrote:I responded "correct" to the part that said "So if it was not checked, it wont even be in $_POST? " (just to clarify)
Still isnt working... unless i misunderstand 'isset'

Code: Select all

$checked = "no";

if (isset($employmentprog)) {
  $checked = "yes"; 
}

if (isset($businessprog)) { 
$checked = "yes"; 
} 

if (isset($certprog)) { 
$checked = "yes"; 
} 

if (isset($youthprogg)) { 
$checked = "yes"; 
} 

if (isset($parentprog)) { 
$checked = "yes"; 
} 

if (isset($policyprog)) { 
$checked = "yes"; 
} 

if ($checked = 'no') 
{
echo "Please use your browser to go back and select at least one program.";
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$employmentprog is not the same as $_POST['employmentprog'].

If you have register_globals on, ignore the setting and work as if they were off.
jmilane
Forum Commoner
Posts: 89
Joined: Mon Aug 07, 2006 6:05 pm

Post by jmilane »

feyd wrote:$employmentprog is not the same as $_POST['employmentprog'].

If you have register_globals on, ignore the setting and work as if they were off.
I went through and did

Code: Select all

$employmentprog = $_POST['employmentprog']
Just to make my life easier...
jmilane
Forum Commoner
Posts: 89
Joined: Mon Aug 07, 2006 6:05 pm

Post by jmilane »

jmilane wrote:
feyd wrote:$employmentprog is not the same as $_POST['employmentprog'].

If you have register_globals on, ignore the setting and work as if they were off.
I went through and did

Code: Select all

$employmentprog = $_POST['employmentprog']
Just to make my life easier...
Actually, then all of these are going to be set, because I set them.

Oh no.

Still not working:

Code: Select all

$checked = "no";

if (isset($_POST['employment_chkbox'])) {
  $checked = "yes"; 
}

if (isset($_POST['business_chkbox'])) { 
$checked = "yes"; 
} 

if (isset($_POST['certification_chkbox'])) { 
$checked = "yes"; 
} 

if (isset($_POST['youth_chkbox'])) { 
$checked = "yes"; 
} 

if (isset($_POST['parent_chkbox'])) { 
$checked = "yes"; 
} 

if (isset($_POST['advocacy_chkbox'])) { 
$checked = "yes"; 
} 

if ($checked = "no") 
{
echo "Please use your browser to go back and select at least one program.";
}
Maybe this will help (the code in the form that calls this):

Thank you, by the way...

Code: Select all

<input name="employment_chkbox" type="checkbox" value="Y" id="qf_085b1c" /><label for="qf_085b1c">
Employment Services
</label>&nbsp;<br>
<input name="business_chkbox" type="checkbox" value="Y" id="qf_576ab8" /><label for="qf_576ab8">
Minority Business Services
</label>&nbsp;<br><input name="certification_chkbox" type="checkbox" value="Y" id="qf_87e3e6" /><label for="qf_87e3e6">
Technical Certification
</label>&nbsp;<br><input name="youth_chkbox" type="checkbox" value="Y" id="qf_b2b549" /><label for="qf_b2b549">
Youth Services
</label>&nbsp;<br><input name="parent_chkbox" type="checkbox" value="Y" id="qf_797412" /><label for="qf_797412">
Parent Services
</label>&nbsp;<br><input name="advocacy_chkbox" type="checkbox" value="Y" id="qf_e01cd9" /><label for="qf_e01cd9">
Public Policy / Advocacy
</label></td>
Post Reply