Page 1 of 1
how to check if variable exist?
Posted: Mon Dec 07, 2009 8:45 am
by viarex
1) I have a list of checkboxes in a form however user is not required to check all of them
<input type="checkbox" value="1" name="pup_English" />
<input type="checkbox" value="1" name="pup_Chinese" />
2) After submitting the form i want to check which boxes are checked.
For example pup_English is checked and pup_Chinese is not checked
3) I coded:
if ($_POST['pup_English'] != '') {
echo "blah"}
if ($_POST['pup_Chinese'] != '') {
echo "blah"}
i hit an error :Undefined index: pup_Chinese in F:\wamp\www\reg_check.php on line 4
how do i check if the variable exist?
Re: how to check if variable exist?
Posted: Mon Dec 07, 2009 8:56 am
by Grizzzzzzzzzz
viarex wrote:1) I have a list of checkboxes in a form however user is not required to check all of them
<input type="checkbox" value="1" name="pup_English" />
<input type="checkbox" value="1" name="pup_Chinese" />
2) After submitting the form i want to check which boxes are checked.
For example pup_English is checked and pup_Chinese is not checked
3) I coded:
if ($_POST['pup_English'] != '') {
echo "blah"}
if ($_POST['pup_Chinese'] != '') {
echo "blah"}
i hit an error :Undefined index: pup_Chinese in F:\wamp\www\reg_check.php on line 4
how do i check if the variable exist?
think it's simply just
Re: how to check if variable exist?
Posted: Mon Dec 07, 2009 9:06 am
by Apollo
Actually, it's
To safely determine if a checkbox is actually checked, you can use
Code: Select all
$isChecked = isset($_POST['pup_Chinese']) ? $_POST['pup_Chinese'] : false;
Re: how to check if variable exist?
Posted: Mon Dec 07, 2009 10:56 am
by jackpf
Code: Select all
$isSet = (bool) /*if you want to be explicit about what it's doing...*/ (isset($_POST['pup_Chinese']));
Nice and short.
Re: how to check if variable exist?
Posted: Mon Dec 07, 2009 11:04 am
by Apollo
jackpf wrote:Code: Select all
$isSet = (bool) /*if you want to be explicit about what it's doing...*/ (isset($_POST['pup_Chinese']));
Nice and short.
Yes, but beware... I deliberately did not rely on isset alone, because if you use the same on a text or other input field, the value may be set but empty, and usually you want to consider empty input fields as 'missing'.
Re: how to check if variable exist?
Posted: Mon Dec 07, 2009 11:53 am
by viarex
ah thx thx. im a newbie in this.
Re: how to check if variable exist?
Posted: Mon Dec 07, 2009 3:50 pm
by jackpf
I don't think browsers send the checkbox at all if it isn't checked though..
Re: how to check if variable exist?
Posted: Mon Dec 07, 2009 11:16 pm
by harrymanjan
always works for checkbox, radio, textbox:
if($_POST['pup_Chinese'] == null)
{
echo 'null';
}
else
{
$pup_chinese = $_POST['pup_Chinese'];
echo $pup_chinese;
}
Re: how to check if variable exist?
Posted: Tue Dec 08, 2009 3:21 am
by Apollo
jackpf wrote:I don't think browsers send the checkbox at all if it isn't checked though..
No, true, they don't indeed, my consideration was only in case he applies the same constructions on other fields (that do get sent even when they're empty).
harrymanjan wrote:if($_POST['pup_Chinese'] == null)
I think on some older / arcane installations of PHP, this may result in 'undefined index'.
Re: how to check if variable exist?
Posted: Tue Dec 08, 2009 4:17 am
by jackpf
Oh right, fair enough.
harrymanjan wrote:always works for checkbox, radio, textbox:
if($_POST['pup_Chinese'] == null)
{
echo 'null';
}
else
{
$pup_chinese = $_POST['pup_Chinese'];
echo $pup_chinese;
}
Yeah, that would generate an E_NOTICE error if the field wasn't sent. You need to use empty() or isset(), which don't generate errors.