简体   繁体   English

PHP Javascript Uncaught SyntaxError: Unexpected token ILLEGAL

[英]PHP Javascript Uncaught SyntaxError: Unexpected token ILLEGAL

I have this PHP script and it does not work properly.我有这个 PHP 脚本,但它不能正常工作。 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:有消息,但 JavaScript 没有把它放在 HTML div 中:

div.aler

I get this error message in the Chrome console:我在 Chrome 控制台中收到此错误消息:

Uncaught SyntaxError: Unexpected token ILLEGAL未捕获的语法错误:意外的令牌非法

The php output is not escaped for ", so instead of \\" you have to use \\\\\\" or \\'. php 输出没有转义为 ",所以你必须使用 \\\\\\" 或 \\' 而不是 \\"。

Btw json_encode as a string would be much better...顺便说一句 json_encode 作为字符串会好得多......

$("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 "||"您忘记了下面一行中的isset ,因为您使用了“||”,因此需要它(OR) in your first if statement, php throwing an error and thats breaking your javascript (或)在你的第一个 if 语句中,php 抛出一个错误,这破坏了你的 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>'; ?>");

您正试图将其放入 div.alert ……但在您编写的“div.aler”代码中,您缺少一个 T ……

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

当然,在输出之前转义$success$failure

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Uncaught SyntaxError:意外的令牌非法(PHP&Javascript) - Uncaught SyntaxError: Unexpected token ILLEGAL (PHP & Javascript) Uncaught SyntaxError:意外的令牌非法-PHP和Javascript Zend Studio - Uncaught SyntaxError: Unexpected token ILLEGAL - PHP and Javascript Zend Studio Uncaught SyntaxError:意外的令牌非法—将PHP包含在JQuery / JavaScript中时 - Uncaught SyntaxError: Unexpected token ILLEGAL — When including PHP into JQuery/JavaScript PHP / jQuery未捕获的SyntaxError:意外的令牌非法 - PHP/jQuery Uncaught SyntaxError: Unexpected token ILLEGAL Uncaught SyntaxError:意外的令牌非法 - Uncaught SyntaxError: Unexpected token ILLEGAL 未捕获的SyntaxError Unexpected Token非法javascript与txt文件 - Uncaught SyntaxError Unexpected Token illegal javascript with txt file 未捕获的SyntaxError:意外的令牌ILLEGAL load-scripts.php:1 - Uncaught SyntaxError: Unexpected token ILLEGAL load-scripts.php:1 ExtJS / PHP / MySQL:未捕获的语法错误:意外的令牌非法 - ExtJS/PHP/MySQL: Uncaught SyntaxError: Unexpected token ILLEGAL jQuery Uncaught SyntaxError:意外的令牌非法 - jquery Uncaught SyntaxError: Unexpected token ILLEGAL Chrome浏览器检查器:未捕获的语法错误:意外的令牌非法 - Chrome Inspector: Uncaught SyntaxError: Unexpected token ILLEGAL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM