0

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. :(

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Mohammad Areeb Siddiqui
  • 9,795
  • 14
  • 71
  • 113
  • Are you sure the error is on this line? – Bryan Feb 18 '14 at 20:54
  • btw even if you fix your php error, this will never work like this. with, javascript will have too many errors because of your ' – CodeBird Feb 18 '14 at 20:57
  • 1
    Thats pretty horrible code ... what's wrong with not using echo and doing `>= $ whatever ?><` ? Or even sprintf? – AD7six Feb 18 '14 at 21:00
  • 1
    Your main problem is that you're trying to use single quotes to delimit string in Javascript, and also to delimit HTML onClick attribute that's storing the Javascript. I'd probably swap the `onclick='...'` for `onclick=\"...\"`; i.e. used escaped double quotes. That way the single quotes inside won't terminate the onclick attribute, and the double-quotes won't terminate the PHP string. – Matt Gibson Feb 18 '14 at 21:03
  • 2
    But, overall, this is in some serious need of a change of approach. This mixing of HTML, PHP and Javascript is pretty scary. Use the more standard template-style approach to PHP output, and perhaps don't generate Javascript from the PHP at all, but add it later, unobtrusively. – Matt Gibson Feb 18 '14 at 21:07
  • Possible duplicate of [PHP parse/syntax errors; and how to solve them?](https://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them) – miken32 Mar 30 '19 at 22:10

5 Answers5

2

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

feeela
  • 29,399
  • 7
  • 59
  • 71
1

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

$topic = $result['Topic'];

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

Hanlet Escaño
  • 17,114
  • 8
  • 52
  • 75
0

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/>";
user1477388
  • 20,790
  • 32
  • 144
  • 264
0

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/>';
CodeBird
  • 3,883
  • 2
  • 20
  • 35
0

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;
miken32
  • 42,008
  • 16
  • 111
  • 154