spacepoet Posted March 15, 2012 Share Posted March 15, 2012 Hi: I am stuck on one little glitch with an IF ELSE statement .. hoping someone with fresh eyes can see what I am missing. The checkbox will not stay checked, and I'm over looking something but fail to see it. <? if ($id != "4" ) { echo "<input type='checkbox' name='myPageActive' value='Yes' <?php if($myPageActive == 'Yes') echo 'checked'; ?>> Yes"; } else { echo "<span class='myDeleteMessage'>Homepage must stay active</span>"; } ?> It seems to have something to do with this part: <?php if($myPageActive == 'Yes') echo 'checked'; ?>> Yes"; Can anyone see what I am missing? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/258966-stuck-on-if-else-statement/ Share on other sites More sharing options...
ManiacDan Posted March 15, 2012 Share Posted March 15, 2012 You're probably missing the code that makes $myPageActive into a real variable. Does your code say $myPageActive = $_POST['myPageActive'] anywhere? If not, you need it. If you're working under this assumption that you won't need to do this, stop right now. The behavior that used to do this was called register_globals, and it was dirty and wrong and has been removed from PHP entirely. You must use $_POST directly. Quote Link to comment https://forums.phpfreaks.com/topic/258966-stuck-on-if-else-statement/#findComment-1327541 Share on other sites More sharing options...
spacepoet Posted March 15, 2012 Author Share Posted March 15, 2012 Hi: No, that's not it but thanks for the input .. I had it like this: <input type='checkbox' name='myPageActive' value='Yes' <?php if($myPageActive == 'Yes') echo 'checked'; ?>> Yes and it works fine .. It's just when I tried to put it into the IF ELSE statement that it fails ... Any other ideas? Quote Link to comment https://forums.phpfreaks.com/topic/258966-stuck-on-if-else-statement/#findComment-1327548 Share on other sites More sharing options...
kicken Posted March 15, 2012 Share Posted March 15, 2012 Your already in PHP mode as part of your outter if statement. As such your <?php ... ?> tags are not counted as php tags. Since your code is contained within a string as part of an echo, it is not run. What you need to do is either leave PHP mode or use put the checked value into a variable and use that in your echo. <?php if ($id != "4" ) { $checked = $myPageActive=='Yes'?'checked':''; echo "<input type='checkbox' name='myPageActive' value='Yes' $checked> Yes"; } else { echo "<span class='myDeleteMessage'>Homepage must stay active</span>"; } ?> As a side note, you should always use <?php, never the short form of <? as it's possible for that tag format to be disabled which would render all your code using it useless. Quote Link to comment https://forums.phpfreaks.com/topic/258966-stuck-on-if-else-statement/#findComment-1327552 Share on other sites More sharing options...
spacepoet Posted March 15, 2012 Author Share Posted March 15, 2012 Excellent! looks like that is the fix. i will give it a shot. thanks much. Quote Link to comment https://forums.phpfreaks.com/topic/258966-stuck-on-if-else-statement/#findComment-1327557 Share on other sites More sharing options...
ManiacDan Posted March 15, 2012 Share Posted March 15, 2012 Good eye kicken. Quote Link to comment https://forums.phpfreaks.com/topic/258966-stuck-on-if-else-statement/#findComment-1327658 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.