Forums

CheckBox and PHP

Killy 09 Jun, 2009
Hello,

I have 2 checkBoxes in a form :

{check0} and {check1} with just one possibility for each one. For exemple:

"Are you a smoker?" [ ] Yes ({check0})
"Will you take the train?" [ ] Yes ({check1})

And that's it.

What I would like to do is :

<?php
$check1 = "{check1}";
if ($check1 == "Yes")
{
echo "Yes";
}else{
echo "No";
}
?>


It always answer "No".
But it's not working, I tried everything even using directly {check1} instead of $check1.
If I use some echo to see what's in {check1} it answer "Yes" if the CheckBox was checked in the form and {check1} if it was not.
To me it makes no sense, any help would be great, I've spent all my monday on this πŸ˜‘
GreyHead 09 Jun, 2009
Hi Killy,

Try
<?php
if ( {$check1} )
{
echo "Yes";
}else{
echo "No";
}
?>

Bob

PS Yes/No radio button pairs make this easier.
Killy 09 Jun, 2009
Thanks for the answer (you're fast πŸ˜€ ).

Should not this be more like that :

<?php
if ( "{check1}" )
{
echo "Yes";
}else{
echo "No";
}
?>


Either way it gives a "Yes" everytime now.

(i'll try the Yes/No radio button, but still I'm curious about this)
GreyHead 09 Jun, 2009
Hi Killy,

Sorry there was a typo in there but not that one, try this (no $, no quote):
<?php
if ( {check1} )
{
echo "Yes";
}else{
echo "No";
}
?>

Bob
Killy 09 Jun, 2009
Parse error !

I guess it does not like the {}.

I give up anyway, i wrote something with some radio buttons. Could you give me the code correspondig please?

I tried to test it with {radio00} {radio01} but it returns just {radio00}{radio01}.

edit: Nevermind {radio0} did the trick ! Thanks againπŸ™‚
GreyHead 09 Jun, 2009
Hi Killy,

Great, the other option can't use the short-hand then (I'm never certain when it's allowable), it would be
<?php
$check1 = JRequest::getInt('check1', '', 'post');
if ( $check1 )
{
echo "Yes";
}else{
echo "No";
}
?>

Bob
This topic is locked and no more replies can be posted.