Page 1 of 1

Q: if statement, form, & echo variables in html

Posted: Tue Feb 07, 2012 9:01 am
by mondsteigen
I have a working program that looks basically as follow:

Code: Select all

<html>
<body> 
   --- some stuff ---
<div>
<?php 
echo '<form method="post">';
   --- some <input>
   echo '<form type="submit">';

if(condition)
   echo 'Fine.';
?>
</div>
</body>
</html>
I want to print 'Fine' at the beginning, right above the div tag. So I made the following change:

Code: Select all

<html>
<body> 
   --- some stuff ---
<? echo $fine; ?>
<div>
<?php 
echo '<form method="post">';
   --- some <input>
   echo '<form type="submit">';

if(condition)
   $fine = 'Fine.';
?>
</div>
</body>
</html>
This is not working. Could someone tell me what's wrong with my code and how to fix it? Thank you.

Re: Q: if statement, form, & echo variables in html

Posted: Tue Feb 07, 2012 9:06 am
by Celauran
You're trying to echo a variable that hasn't been defined.

Code: Select all

<html>
<body> 
   --- some stuff ---
<?php if (condition): ?>
Fine.
<?php endif; ?>
<?php 
echo '<form method="post">';
   --- some <input>
   echo '<form type="submit">';
?>

</body>
</html>

Re: Q: if statement, form, & echo variables in html

Posted: Tue Feb 07, 2012 9:22 am
by mondsteigen
Thank you very much for the quick response and the explanation AND the correct code!

Re: Q: if statement, form, & echo variables in html

Posted: Tue Feb 07, 2012 9:32 am
by Celauran
Of course it works. Make sure you put a valid condition.

Code: Select all

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Debug</title>
    </head>
    <body>
        <?php if (date('w') == 2): ?>
        Fine.
        <?php endif; ?>
    </body>
</html>

Re: Q: if statement, form, & echo variables in html

Posted: Tue Feb 07, 2012 9:06 pm
by mondsteigen
Thanks. The condition involved another variable, so I had to add that bit to the code you gave me.

Re: Q: if statement, form, & echo variables in html

Posted: Tue Feb 07, 2012 9:13 pm
by Celauran
Glad you got it working.

Re: Q: if statement, form, & echo variables in html

Posted: Thu Feb 09, 2012 5:15 pm
by mondsteigen
Celauran wrote:You're trying to echo a variable that hasn't been defined.
How about this?
I want to print the string variable $warning at the beginning of the form. I thought $warning is defined because it's in the same php section as the rest of the php code. But it doesn't work. I get no error message, but the string $warning is not printed. The rest of the code works fine though. The variable is still not defined in this case?

Code: Select all

<html>
<body>
   --- some stuff ---
<?php
echo $warning;
echo '<form method="post">';
   --- some <input>---

foreach ($_POST as $value){
   if (isset ($value)){ 
      $done++;
   }
}

if ($done !=3)
   echo '<input type="submit" name="submit" value="Submit Your Answers!" /><br/><br/>';
  
if (($done > 0)&&($done < 3))
   $warning = '<p class="warning">You must answer ALL questions in order to
 receive a score.</p>';

if($done==3){
   if ($correct==0){
      $correct="0";
      echo "<p class='score'>You got $correct question(s) right!</p>";
      echo "<p class='score'>Your score is 0.</p>";
   } else{
       $score=intval(($correct/($done-1))*100);
       echo "<p class='score'>You got $correct question(s) right!</p>";
       echo "<p class='score'>Your score is $score.</p>";
     }
   echo '<input type="submit" name="Reset" onclick="resetForm();" value="Try again">'; 
}
echo '</form>';
?>
</body>
</html>

Re: Q: if statement, form, & echo variables in html

Posted: Thu Feb 09, 2012 7:09 pm
by Celauran
Same thing, yes. You're echoing $warning up at the top of the script, but only defining it with $warning = much farther down. Why not move your PHP code to the top of the file? $warning would then be defined before you need to call it. Since it only appears to be set under certain conditions, I'd also enclose the echo in an if (isset($warning)) block.

Re: Q: if statement, form, & echo variables in html

Posted: Fri Feb 10, 2012 4:24 pm
by mondsteigen
Celauran wrote: Why not move your PHP code to the top of the file? $warning would then be defined before you need to call it.
I'm not sure what you mean by 'the top of the file,' but when I moved the 'foreach' block above the <input> block, the maths block (the last if block) did no longer work. It gives me 0 always.
Celauran wrote: Since it only appears to be set under certain conditions, I'd also enclose the echo in an if (isset($warning)) block.

What do you mean by 'enclose the echo in an if (isset($warning)) block?