how to check if variable exist?
Moderator: General Moderators
how to check if variable exist?
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?
<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?
- Grizzzzzzzzzz
- Forum Contributor
- Posts: 125
- Joined: Wed Sep 02, 2009 8:51 am
Re: how to check if variable exist?
think it's simply justviarex 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?
Code: Select all
if ($_POST['pup_Chinese'])
Re: how to check if variable exist?
Actually, it's
To safely determine if a checkbox is actually checked, you can use
Code: Select all
if (isset($_POST['pup_Chinese']))Code: Select all
$isChecked = isset($_POST['pup_Chinese']) ? $_POST['pup_Chinese'] : false;Re: how to check if variable exist?
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?
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'.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.
Re: how to check if variable exist?
ah thx thx. im a newbie in this.
Re: how to check if variable exist?
I don't think browsers send the checkbox at all if it isn't checked though..
-
harrymanjan
- Forum Newbie
- Posts: 2
- Joined: Mon Dec 07, 2009 11:03 pm
Re: how to check if variable exist?
always works for checkbox, radio, textbox:
if($_POST['pup_Chinese'] == null)
{
echo 'null';
}
else
{
$pup_chinese = $_POST['pup_Chinese'];
echo $pup_chinese;
}
if($_POST['pup_Chinese'] == null)
{
echo 'null';
}
else
{
$pup_chinese = $_POST['pup_Chinese'];
echo $pup_chinese;
}
Re: how to check if variable exist?
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).jackpf wrote:I don't think browsers send the checkbox at all if it isn't checked though..
I think on some older / arcane installations of PHP, this may result in 'undefined index'.harrymanjan wrote:if($_POST['pup_Chinese'] == null)
Re: how to check if variable exist?
Oh right, fair enough.
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.harrymanjan wrote:always works for checkbox, radio, textbox:
if($_POST['pup_Chinese'] == null)
{
echo 'null';
}
else
{
$pup_chinese = $_POST['pup_Chinese'];
echo $pup_chinese;
}