0

I've looked a bit into this error and it seems that a majority of people with a similar question end up being the victim of invisible characters. However, I've taken out any white space and retyped the code by hand and I'm still getting this error.

I have singled the precise bit that induces the error, code below.

PHP

printf("<td><button class=\"btn btn-primary btn-mini\" onclick=\"viewservice(%s)\">View</button></td>",$RadAcct_List_Obj->User_List[$i]['USERNAME']);

Javascript

function viewservice(n)
{
    alert(n);
}

This results in the Uncaught SyntaxError. However, if I remove the %s placeholder and the call at the end, and leave it as just this

printf("<td><button class=\"btn btn-primary btn-mini\" onclick=\"viewservice()\">View</button></td>");

Then it works fine. I don't understand why it's failing here, a few lines above this I am using similar code (below) and it's working fine.

printf("<td>%s</td>",$RadAcct_List_Obj->User_List[$i]['USERNAME']);

1 Answer 1

4

You need quotes around your string literal in the JS:

printf("<td><button class=\"btn btn-primary btn-mini\" onclick=\"viewservice('%s') ... etc
                                                                             ^  ^
Sign up to request clarification or add additional context in comments.

2 Comments

Holy cow, thank you! Is there any reason why the last snippet of code in my question works without quotes around the string literal? (Also, I will accept your answer as best in 10 minutes). Scratch that, just realized I'm passing the function a string and it should be in quotes. Damn I'm bad.
@Matt - Any time :) Your last snippet works because it's not Javascript, it's just inside a <td> tag. In JS, to specify a string literal, it needs to be surrounded with quotes. For example, var x = 'string'; would be OK, var x = string; is not.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.