Mohammad Areeb Siddiqui
Mohammad Areeb Siddiqui

Reputation: 10179

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?

Here's my 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. :(

Upvotes: 0

Views: 1308

Answers (5)

feeela
feeela

Reputation: 29932

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. That way your JS will probably not work too. 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. Maybe a template engine is useful for you. The simplest version would be to use sprintf to output that kind of strings:

$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). 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:

<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")

Upvotes: 2

miken32
miken32

Reputation: 42676

As mentioned above, you were nesting your single quotes, and complicating your HTML syntax to get around your use of quote marks. If you're outputting this much HTML, heredoc is always helpful. And if your editor is smart like mine, it will give you HTML syntax highlighting within the block!

    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;

Upvotes: 0

CodeBird
CodeBird

Reputation: 3858

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/>';

Upvotes: 0

user1477388
user1477388

Reputation: 21430

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/>";

Upvotes: 0

Hanlet Esca&#241;o
Hanlet Esca&#241;o

Reputation: 17370

Your problems are these guys $result['Topic']}'' Try something like:

$topic = $result['Topic'];

and then use it topic=$topic in your code.

Upvotes: 1

Related Questions