2

I am having a problem with the following piece of code:

$submit = $_POST['submit'];
$answer = "8";
$input = strip_tags($_POST['input']);
if ($submit){ 
    if ($input==$answer){
        echo "Correct";
    }
else
    echo "Wrong";

CSS:

.wrong {      
margin-top: 5px;       
padding: 5px;    
background-color:#F00;   
border: 2px solid #666;    
width:auto;
color: #000000;   
}

All I want is to put a little bit of CSS in with an PHP echo command. If the user gets the answer wrong a red box should appear with "Wrong" in the middle.

I have already tried

echo <div class="wrong">"Wrong"</div>;

but that did not work.

2
  • Looks like it works to me. What exactly is the problem? Commented Sep 18, 2011 at 14:21
  • im trying to get it to display a red box around an echo command, now it is simple to do within html but i need to do it with php the error i get when i run it is Parse error: syntax error, unexpected '<' in /hermes/web01b/b2746/moo.roundaboutmkcom/roundaboutmk/coding/index.php on line 25 Commented Sep 18, 2011 at 14:25

2 Answers 2

10

PHP interprets the quote character special, it marks the start or end of a string literal. Either escape your quotes using a backslash or use other single quotes:

<style>
.wrong {
    margin-top: 5px;
    padding: 5px;
    background-color: #F00;
    border: 2px solid #666;
    width: auto;
    color: #000000;
}
</style>
<?php
$submit = $_POST['submit'];
$answer = "8";
$input = strip_tags($_POST['input']);
if ($submit) {
    if ($input == $answer) {
        echo "Correct";
    } else {
        // note: escaped the quote character using a backslash
        echo "<div class=\"wrong\">wrong</div>";
        // alternative:
        //echo '<div class="wrong">wrong</div>';
    }
}
?>

See also the PHP manual on the string type.

Sign up to request clarification or add additional context in comments.

Comments

3

please try as:

echo '<div class="wrong">Wrong</div>';

missing quotes for echo statement.

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.