0

I have the following code:

print "<TR><TD>".$data->pass_name."</TD><TD><span id='credit'>".$data->credit_left."</span></TD><TD><input type='button' value='- 1' onclick='removeOneCredit(pass_id\=".$data->pass_id."&credit_left\=".$data->credit_left.");'></TD></TR>\n";

where I am trying to escape the equal so it gets passed in the function as it need to be a query in the url (the rendered code will be like this:

<input type='button' value='- 1' onclick='removeOneCredit(pass_id\=5&credit_left\=2");'>

But I keep getting errors that the \ that I use to escape my = is invalid. How would I escape my = so it can be passed into the argument of my js function?

Thank you

5
  • what you trying to acheive with this ? onclick should have correct javascript syntax...
    – zb'
    Dec 10, 2012 at 2:47
  • I don't think you need to escape the equal sign.
    – Alvin Wong
    Dec 10, 2012 at 2:53
  • I am passing a query strings as an argument to an ajax function that passes that argument into a url that a php file executes Dec 10, 2012 at 2:55
  • If I don't escape the equal sign I get errors "invalid left hand side arguments" Dec 10, 2012 at 2:55
  • What you need to do is to quote the query string, like @Musa's answer.
    – Alvin Wong
    Dec 10, 2012 at 2:56

2 Answers 2

2

You don't need to escape the =, what you need to do is quote the string.

print "<TR><TD>".$data->pass_name."</TD><TD><span id='credit'>".$data->credit_left."</span></TD>
       <TD><input type='button' value='- 1' onclick='removeOneCredit(\"pass_id=".$data->pass_id."&credit_left=".$data->credit_left."\");'></TD></TR>\n";
2
  • problem is in php it is part of print so I can't use double quotes: print "<TR><TD>".$data->pass_name."</TD><TD><span id='credit'>".$data->credit_left."</span></TD><TD><input type='button' value='- 1' onclick='removeOneCredit(pass_id\=".$data->pass_id."&credit_left\=".$data->credit_left.");'></TD></TR>\n"; Dec 10, 2012 at 2:50
  • @bamwebdesign You can just escape it.
    – Musa
    Dec 10, 2012 at 2:55
0

You forgot to put quotes around the string you are passing to removeOneCredit

<input ... onclick='removeOneCredit("pass_id=5&credit_left=2");'>
2
  • I can't because it is inside a print statement in php (See code in original question) Dec 10, 2012 at 2:54
  • @bamwebdesign: if so, you escape the quote (\" or \'), not the equals sign
    – cegfault
    Dec 10, 2012 at 2:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.