jlagomarsini
jlagomarsini

Reputation: 21

SyntaxError: identifier starts immediately after numeric literal. Passing php variable to JavaScript

I am trying to pass in two variables to a JavaScript function: the input itself and the user id. I call the function using the onclick attribute of the input.

 echo "<input type = 'submit' class = 'replyButton' id = 'replyButton'
 value = 'reply' onclick = 'insertComment(this, $user_id);'>";

The variables go into a JavaScript function:

function insertComment(input, user)
{
     //use input and user id to carry out tasks.
} 

The error message given is at the function call to 'insertComment()' in the onclick of the input. The error message looks like this:

SyntaxError: identifier starts immediately after numeric literal    

       insertComment(this, 9f60d869342a);

I have tried multiple quote and dot combinations and have checked the other posts on StackOverFlow. They were all to no avail. Your help in resolving this error is greatly appreciated.

Upvotes: 2

Views: 14597

Answers (1)

Patrick Evans
Patrick Evans

Reputation: 42756

you need to quote the user id

echo "<input type = 'submit' class = 'replyButton' id = 'replyButton'
 value = 'reply' onclick = 'insertComment(this, \"$user_id\");'>";

Upvotes: 5

Related Questions