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
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>');
};?>
);
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>'; ?>");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With