-
if else Trouble
HI all,
I have this snippet of code:
PHP Code:
<P>Visible:<BR>
<input type=radio name="visible" value="Y" <?php if ($visible == Y) {
echo("CHECKED");
} else {
echo("");
}?>>Yes <input type=radio name="visible" value="N" <?php if ($visible == N) {
echo("CHECKED");
} else {
echo("");
}?>>No</P>
This is part of a larger script that connects to the database and a certain table. Then, I have the above code. You can probably figure out what it's supposed to do.
I'm not getting errors, but it is not printing CHECKED at all. I'm positive that the database has this registered (as opposed to being an empty field.)
Any help is greatly appreciated.
-Corbb
-
Maybe quotes on the Y and N values? I'm no expert...
-
TechSited,
Thanks for your help, but still the same problem.
-
Try this
PHP Code:
<?php
//pull it from your table column (I called it visible in this example)
$visible=$myrow["visible"];
//use switch to determine what value is in the cell
switch ($visible) {
//if yes mark the yes as checked
case"Y":
$visibleY=("checked=\"checked\"");
break;
//if no mark no as checked
case "N":
$visibleN=("checked=\"checked\"");
break;
}
//then spit it out
?>
<input type="radio" value="Y" name="visible" <? echo $visibleY ?> />Yes Please<br />
<input type="radio" value="N" name="visible" <? echo $visibleN ?> />No thanks<br />
I've trimmed this down from a chuck of code where there were at elast a dozen choices - it worked fine, it should work with just your two - though there is probably a more efficient way to do it than by using switch
HTH
Stuart
-
Hi Stuart,
Thanks for your help. However, I'm still gettting the same problem. Here's my code:
PHP Code:
<P>Visible:<BR>
<?php
//use switch to determine what value is in the cell
switch ($visible) {
//if yes mark the yes as checked
case"Y":
$visibleY=("checked=\"checked\"");
break;
//if no mark no as checked
case "N":
$visibleN=("checked=\"checked\"");
break;
}
?>
<input type="radio" value="Y" name="visible" <? echo $visibleY ?> />Yes
<input type="radio" value="N" name="visible" <? echo $visibleN ?> />No</P>
-
echo the contents of $visible out somewhere that you can see what it's value is.
-
Asking a dumb question just in case, there's no chance you have a another checked radio button later on the page, with the the name "visible"?
The last button checked in the group overides all previous.
-
Thanks everybody for your help.
I got it fixed.
I had accidentally named my variable $isvisible instead of $visible.
Thanks again for all of your help,
-Corbb