Page 1 of 2

checking if checkboxes are checked

Posted: Thu Sep 21, 2006 3:54 pm
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!

Posted: Thu Sep 21, 2006 4:06 pm
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>

Posted: Thu Sep 21, 2006 4:12 pm
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.

Posted: Thu Sep 21, 2006 4:12 pm
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!

Posted: Thu Sep 21, 2006 4:14 pm
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?

Posted: Thu Sep 21, 2006 4:15 pm
by Luke
correct

Posted: Thu Sep 21, 2006 4:17 pm
by jmilane
The Ninja Space Goat wrote:correct
if ( isset($employmentprog)) {
$checked = "yes";
}

something like this, then?

Posted: Thu Sep 21, 2006 4:20 pm
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.

Posted: Thu Sep 21, 2006 4:20 pm
by jmilane
aaah!!!

T_PAAMAYIM_NEKUDOTAYIM ?????

Posted: Thu Sep 21, 2006 4:22 pm
by feyd
Translates to "double colon" ..

Posted: Thu Sep 21, 2006 4:24 pm
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)

Posted: Thu Sep 21, 2006 4:25 pm
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.";
}

Posted: Thu Sep 21, 2006 4:29 pm
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.

Posted: Thu Sep 21, 2006 4:31 pm
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...

Posted: Thu Sep 21, 2006 4:35 pm
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>