4

I have this PHP script and it does not work properly. What is the mistake?

<?php
if ( isset($success) || isset($failure) ) {
?>
<script>
    $(document).ready(function(){
        $('div.aler').css('display','block');
        $("div.aler").html("<?php if($success){echo '<p class=\"success\">'.$success.'</p>';} elseif($failure) {echo '<p class=\"failure\">'.$failure.'</p>';}; ?>");
        setTimeout(function(){
            $("div.aler").fadeOut("slow", function (){
                $("div.aler").remove();
            });
        }, 5000);
    });
</script>
<?php }  

I think there must be a problem with quotes:

" . $failure

has the message, but the JavaScript does not put it in HTML div:

div.aler

I get this error message in the Chrome console:

Uncaught SyntaxError: Unexpected token ILLEGAL

4
  • 1
    You have to double encode your quotes, \\", so that the result contains \" for the JavaScript string. Look at the generated HTML and you will see. Commented Jun 22, 2013 at 17:50
  • 1
    Try escaping any " inside PHP like this \\" see what that does. Or change them to single quotes '. Or maybe even \". Commented Jun 22, 2013 at 17:50
  • @FelixKling, It puts nothing to the div, so I can not check using html source. but I will give it a try using double encode my quotes. Thanks Commented Jun 22, 2013 at 18:26
  • @FelixKling I double encoded the " s but nothing changed Commented Jun 22, 2013 at 18:28

4 Answers 4

6

The php output is not escaped for ", so instead of \" you have to use \\\" or \'.

Btw json_encode as a string would be much better...

$("div.aler").html(<?php
    if($success){
        echo json_encode('<p class="success">'.$success.'</p>');
    }
    elseif($failure){
        echo json_encode('<p class="failure">'.$failure.'</p>');
    };?>
);
Sign up to request clarification or add additional context in comments.

1 Comment

@Arash The problem is with your copy-paste skills, not with this code. (I tested before I published.)
2

you forgot isset in below line, its required since your using "||" (OR) in your first if statement, php throwing an error and thats breaking your javascript

$("div.aler").html( "<?php if( $success ){ echo '<p class=\"success\">'.$success.'</p>';}elseif($failure){echo '<p class=\"failure\">'.$failure.'</p>';}; ?>");

change this to...

$("div.aler").html( "<?php echo ( isset( $success ) ) ? '<p class=\"success\">'.$success.'</p>' : '<p class=\"failure\">'.$failure.'</p>'; ?>");

2 Comments

yeah and also dont forget to escape the success or failure message quotes, use 'addslashes'
Ye this clearly depends on wrong server settings. (display_error etc...) Voted up because I got the undefined variable error message, but I declared the variables after that. Did not know that it is possible to not debug something like that...
1

You're trying to put it into div.alert...yet in your code you wrote "div.aler" you're missing a T...

Comments

0
$("div.aler").html("<p class='<?=$success? 'success' : 'failure'?>'><?=$success? $success : $failure?></p>");

And of course escaping $success and $failure before output.

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.