how to check if variable exist?

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
viarex
Forum Newbie
Posts: 2
Joined: Mon Dec 07, 2009 8:37 am

how to check if variable exist?

Post 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?
User avatar
Grizzzzzzzzzz
Forum Contributor
Posts: 125
Joined: Wed Sep 02, 2009 8:51 am

Re: how to check if variable exist?

Post 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

Code: Select all

 
if ($_POST['pup_Chinese'])
 
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: how to check if variable exist?

Post by Apollo »

Actually, it's

Code: Select all

if (isset($_POST['pup_Chinese']))
To safely determine if a checkbox is actually checked, you can use

Code: Select all

$isChecked = isset($_POST['pup_Chinese']) ? $_POST['pup_Chinese'] : false;
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: how to check if variable exist?

Post by jackpf »

Code: Select all

$isSet = (bool) /*if you want to be explicit about what it's doing...*/ (isset($_POST['pup_Chinese']));
:D

Nice and short.
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: how to check if variable exist?

Post by Apollo »

jackpf wrote:

Code: Select all

$isSet = (bool) /*if you want to be explicit about what it's doing...*/ (isset($_POST['pup_Chinese']));
:D

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'.
viarex
Forum Newbie
Posts: 2
Joined: Mon Dec 07, 2009 8:37 am

Re: how to check if variable exist?

Post by viarex »

ah thx thx. im a newbie in this.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: how to check if variable exist?

Post by jackpf »

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?

Post by harrymanjan »

always works for checkbox, radio, textbox:

if($_POST['pup_Chinese'] == null)
{
echo 'null';
}
else
{
$pup_chinese = $_POST['pup_Chinese'];
echo $pup_chinese;
}
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: how to check if variable exist?

Post 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'.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: how to check if variable exist?

Post 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.
Post Reply