Jump to content

Stuck on IF ELSE statement


spacepoet

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.