3

PHP Part is here So when I post it, else part does not work at all (meaning: the text I set just disappear), while the other half works (meaning: I can assign variables, but can't leave it blank). I have been struggling with it all night...please help! So this is the second page, if you need to take a look at the code on the first page, please let me know!

<?php
$lovers = $_POST['lovers'];
$quote = $_POST['quote'];
$color = $_POST['color'];
$font = $_POST['font'];
$imdblink = $_POST['imdblink'];
?>

<?php
if (isset($_POST['lovers']))  
    {
$lovers = $_POST['lovers'];
    }else{
        echo "P & M";
     }

if (isset($_POST['quote']))  
     {
        $quote = $_POST['quote'];
     }else{
        echo "I love you.";
     }

  if (isset($_POST['color']))  
     {
        $color = $_POST['color'];
     }else{
        echo "yellow";
     }

  if (isset($_POST['font']))  
     {
        $font = $_POST['font'];
     }else{
        echo "Futura";
     }

  if (isset($_POST['imdblink']))  
     {
        $imdblink = $_POST['imdblink'];
     }else{
        echo "http://www.imdb.com/";
     }
?>

Here is the HTML part: Is there anything wrong here? Please help!

<div class="artGroup slide">
  <div class="artwork"> <img src="../_images/M&P.png">
    <div class="detail">
      <div class="movie01c" style="font-family: <?php echo "{$font}"; ?>; font-size: 20px; color: <?php echo "{$color}";?>;">
     <?php echo "{$quote}";?>
      </div>
      <div class="movie01t"><a href="<?php echo "{$imdblink}";?>">
      <?php echo "{$lovers}";?>
      </a></div>
    </div>
  </div>
</div>
4
  • 2
    what do you mean with: "Does not work* ?! Errors ?! Commented Aug 9, 2012 at 11:03
  • Maybe wanna try !empty() instead of isset() Commented Aug 9, 2012 at 11:04
  • @tuxtimo: So supposedly I should get the "else" set value for each of the variable, even if I don't have input for each of them. However, if there is no input, there is no out put at all. Commented Aug 9, 2012 at 11:38
  • @Moak: It's making progress - at least all the "else" values for the variables are showing in the first place, although they still don't show if there is not input for each of them...but thanks! Commented Aug 9, 2012 at 11:39

9 Answers 9

5

Instead of using echo, use variable assignment too in the else part.

if (isset($_POST['lovers'])) {
    $lovers = $_POST['lovers'];
}else{
    $lovers = "P & M";
}
Sign up to request clarification or add additional context in comments.

Comments

4

isset() tells you if the variable exists. If you have a form (which you didn't show the html for), and someone leaves the field blank, the field exists but is empty. There is no way that the form element will not exist after the form is submitted, so isset() will never return FALSE. So you want to use !empty() instead of isset().

Comments

1

your statements should be like...

if (isset($_POST['lovers']) && ($_POST['lovers'] != ''))
{
    $lovers = $_POST['lovers'];
}
else
{
    $lovers = "P & M";
}

1 Comment

it's not really getting any error - so supposedly I should get the "else" set value for each of the variable, even if I don't have input for each of them. However, if there is no input, there is no output at all - it's a total blank.
1

ternary operator will make it really clearer.

for instance:

$imdblink = ((isset($_POST['imdblink'])) ? ($_POST['imdblink']) : ('http://www.imdb.com/'));

furthermore, you should define your default values, and use variable variable. (here an example not using define)

$vars = array('imdblink' => 'http://www.imdb.com/');
foreach ($vars as $varName => $defaultVal)
  $$varName = ((isset($_POST[$varName])) ? ($_POST[$varName]) : ($defaultVal));

Comments

0

You don't want to echo, you want to assign it, then you'll print it in your other PHP script.

I think you're trying to set default values if it doesn't come in the $_POST array. Try

$color = isset($_POST['color']) ? $_POST['color'] : 'yellow';

If you never want a blank color, use

$color = !empty($_POST['color']) ? $_POST['color'] : 'yellow';

Comments

0

This should work

<?php
$lovers = isset($_POST['lovers']) ? $_POST['lovers'] : "P & M" ;
$quote = isset($_POST['quote'])? $_POST['quote'] : "I Love You";
$color = isset($_POST['color'])? $_POST['color'] : "yellow";
$font = isset($_POST['font']) ? $_POST['font'] : "Futura";
$imdblink = isset($_POST['imdblink'])? $_POST['imdblink']: "http://www.imdb.com/";
?>

you can replace isset with !empty if you want to make sure the field is not ... empty

1 Comment

Sorry, you will need to show more code and explain what doesn't work
0

isset returns false only when the variable is not set or it is set to null. it will return true when the variable is ''(empty string) which will be the value you get when no value is entered in that field in the first page.

You need to use empty for this to work properly which will return true for unset and empty vlues. It returns for false for non-empty values. You need to prepend it with !(not operator) to use it here.

And you are using variable assignment in if and echo in else. I think that is what the cause of the problem. Change that echo to assignment.

if (!empty($_POST['lovers']))  
{
    $lovers = $_POST['lovers'];
}else{
    $lovers = "P & M";
}

Don't forget !.

Comments

0

All your isset $_post functions might works thats because it would not come to else part.

Comments

0

Here is an idea. Declare the variable and value first. If the $_POST['lovers'] is not null then change the variable value:

$lovers = "P & M";
if (isset($_POST['lovers']))
{
    $lovers = $_POST['lovers'];
}
echo $lovers;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.