+1 vote
1k views
in Programming by (1.4k points)
Hi all, I am trying to simply call a onClick function on a button and facing this error:

 

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting T_STRING in /home/test/html/home.php on line 121

 

My button html code is:

<button onclick="_gaq.push(['_trackEvent', 'category', 'action', 'opt_label', 1]);"></button>

In this line this error is coming.

I don&rsquo;t know what is happening I am calling _gaq.push on onclick event and this unexpected error is coming, Please give me solution for the same.
closed
0
by
It should specify a line number as well. Probably the error lies 1 line earlier. To determine the actual problem, I would need to see the code fragment. It's virtually impossible for me to determine the problem without seeing it.
Possible errors: a forgotten comma (,), quote(') or semi-colon (;)

1 Answer

+1 vote
by (1.8k points)
selected by
 
Best answer

Hi Jack, this error comes usually when we add apostrophe in between of PHP code, whenever are using any other code like html or java script in PHP I recommend to use slash before the apostrophe.  If we talk about your code I think you are using this button in PHP or printing this button dynamically.

See your code:

<button onclick="_gaq.push(['_trackEvent', 'category', 'action', 'opt_label', 1]);"></button>

You have to just add like:

<button onclick="_gaq.push([\'_trackEvent\',\ 'category\',\ 'action\', \'opt_label\', 1]);"></button>

 

Here slash (\) is saying to code please avoid apostrophe and code compile this line normally.

Try this it would help you.

0
by (1.4k points)
Thanks Dear @Payel its really helped me.
0
by
There is another option, if you really do not want to see backslashes everywhere , you can use heredoc (and nowdoc) syntax, as per PHP.net:

https://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

Not a Member yet?

Ask to Folks Login

My Account

Your feedback is highly appreciated