简体   繁体   English

未捕获的SyntaxError:意外令牌}

[英]Uncaught SyntaxError: Unexpected token }

I am trying to figure out the error since an hour but I am not able to find my mistake!! 一个小时以来我一直在试图找出错误,但我找不到我的错误! Can any one please tell me why am I keep getting Uncaught SyntaxError: Unexpected token } error? 谁能告诉我为什么我不断收到Uncaught SyntaxError: Unexpected token } error?

Here's my PHP: 这是我的PHP:

echo "<p style='color: orange; font-size: 25px;'>{$result['Topic']} - <button type='button' onclick='window.location.href='resources.php?subj={$result['Subject']}'' class='btn btn-sm btn-success pull-right'>Resources</button>&nbsp;<button onclick='window.location.href='questions.php?subj={$result['Subject']}''  class='btn btn-sm btn-success pull-right' style='margin-right: 10px;'>Questions</button> <button onclick='window.location.href='assessment.php?topic={$result['Topic']}''  class='btn btn-sm btn-success pull-right' style='margin-right: 10px;'>Assessment</button></p><hr/>";

Please help. 请帮忙。 :( :(

There are several quotes that are not escaped and even if you do escape the double quotes that currently lead to your error, the rendered HTML markup will still have errors as you are using single quotes for attributes and JavaScript strings inside them. 有几个引号没有被转义,即使您确实转义了当前导致错误的双引号,但是当您对属性和JavaScript字符串使用单引号时,呈现的HTML标记仍然会出错。 That way your JS will probably not work too. 这样,您的JS可能也无法正常工作。 Example: 例:

<button onclick='window.location.href='resources.php?subj=%s''>…</button>

In fact your code is a good example why one should avoid using variables inside a string. 实际上,您的代码就是一个很好的例子,为什么应该避免在字符串中使用变量。 Each time I need to use a double quote in PHP I get the feeling that I did something wrong. 每次我需要在PHP中使用双引号时,都会感觉到我做错了什么。 Maybe a template engine is useful for you. 模板引擎可能对您有用。 The simplest version would be to use sprintf to output that kind of strings: 最简单的版本是使用sprintf输出这种字符串:

$template = <<<EOTEMPLATE
    <p style="color: orange; font-size: 25px;">%s -
        <button type="button" onclick="window.location.href=\'resources.php?subj=%s\'" class="btn btn-sm btn-success pull-right">Resources</button>
        <button onclick="window.location.href='questions.php?subj=%s'" class="btn btn-sm btn-success pull-right">Questions</button>
        <button onclick="window.location.href='assessment.php?topic=%s'" class="btn btn-sm btn-success pull-right">Assessment</button>
    </p>
    <hr/>
EOTEMPLATE;

echo sprintf( $template, $result['Topic'], $result['Subject'], $result['Subject'], $result['Topic'] );

The next optimization iteration would be to remove those inline JS snippets and paste them in a single JS block (or even better: a separate *.js file). 下一个优化迭代将是删除这些内联JS代码片段,并将其粘贴到单个JS块中(甚至更好的是:一个单独的* .js文件)。 But as you are only loading another page, a simple link would do as well. 但是,由于您仅加载另一个页面,因此也可以使用简单的链接。 The following snippet also should do what your input suggests. 以下代码段也应按照您的输入建议进行操作。 (As a link could also be styled to look like a button.) (因为链接的样式也可以看起来像按钮。)

$template = <<<EOTEMPLATE
    <p class="btn-group">%s –
        <a href="resources.php?subj=%s" class="btn btn-sm btn-success pull-right">Resources</a>
        <a href="questions.php?subj=%s" class="btn btn-sm btn-success pull-right">Questions</a>
        <a href="assessment.php?topic=%s" class="btn btn-sm btn-success pull-right">Assessment</a>
    </p>
    <hr/>
EOTEMPLATE;

echo sprintf( $template, $result['Topic'], $result['Subject'], $result['Subject'], $result['Topic'] );

Then, if you still need JavaScript (for example to implement a click tracker for those buttons) you can add an event handler to do so: 然后,如果您仍然需要JavaScript(例如为这些按钮实现点击跟踪器),则可以添加事件处理程序来做到这一点:

<script>
// add an event listener to the document object to capture clicks on existing and future links
document.addEventListener( 'click', function( event ) {
    if( event.target.tagName === 'A' && event.target.classList.has( 'btn-success' ) ) {
        event.preventDefault();

        // do whatever you need to do with that link

        window.location.href = event.target.href;
    }
}, false );
</script>

(This JS example will only work in modern browsers because of the usage of "classList" ) (由于使用了“ classList”,此JS示例仅在现代浏览器中有效

Your problems are these guys $result['Topic']}'' Try something like: 您的问题是这些人$result['Topic']}''尝试以下操作:

$topic = $result['Topic'];

and then use it topic=$topic in your code. 然后在代码中使用topic=$topic

I added some escaped double quotes, try running this: 我添加了一些转义的双引号,请尝试运行此命令:

echo "<p style='color: orange; font-size: 25px;'>{$result['Topic']} - 
<button type='button' onclick='window.location.href=\"resources.php?subj={$result['Subject']}\"' 
class='btn btn-sm btn-success pull-right'>Resources</button>&nbsp;<button onclick='window.location.href=\"questions.php?subj={$result['Subject']}\"'  
class='btn btn-sm btn-success pull-right' style='margin-right: 10px;'>Questions</button> 
<button onclick='window.location.href=\"assessment.php?topic={$result['Topic']}\"'  class='btn btn-sm btn-success pull-right' 
style='margin-right: 10px;'>Assessment</button></p><hr/>";

try it like this 这样尝试

echo '<p style="color: orange; font-size: 25px;">'.$result['Topic'].' - 

    <button type="button" 
    onclick="window.location.href=\'resources.php?subj='.$result['Subject'].'\'" 
    class="btn btn-sm btn-success pull-right">Resources</button>&nbsp;

    <button 
    onclick="window.location.href=\'questions.php?subj='.$result['Subject'].'\'"  
    class="btn btn-sm btn-success pull-right" style="margin-right: 10px;">Questions</button> 

    <button 
    onclick="window.location.href=\'assessment.php?topic='.$result['Topic'].'\'"  
    class="btn btn-sm btn-success pull-right" style="margin-right: 10px;">Assessment</button></p><hr/>';

As mentioned above, you were nesting your single quotes, and complicating your HTML syntax to get around your use of quote marks. 如上所述,您正在嵌套单引号,并使HTML语法复杂化,以避开引号的使用。 If you're outputting this much HTML, heredoc is always helpful. 如果要输出这么多的HTML, heredoc总是很有帮助的。 And if your editor is smart like mine, it will give you HTML syntax highlighting within the block! 而且,如果您的编辑器像我一样聪明,它将在块内为您突出显示HTML语法!

    echo <<< HTML
<p style="color: orange; font-size: 25px;">
    $result[Topic] -
    <button type="button" onclick="window.location.href='resources.php?subj=$result[Subject]'" class="btn btn-sm btn-success pull-right">
        Resources
    </button>
    &nbsp;
    <button onclick="window.location.href='questions.php?subj=$result[Subject]'" class="btn btn-sm btn-success pull-right" style="margin-right: 10px;">
        Questions
    </button>
    &nbsp;
    <button onclick="window.location.href='assessment.php?topic=$result[Topic]'" class="btn btn-sm btn-success pull-right" style="margin-right: 10px;">
        Assessment
    </button>
</p>
<hr/>

HTML;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM