0

I need to output some strings in the onclick anchor function.I have used proper escaping but anyhow its returning error.

My code is:

$output .= '<a title="Minus this" href="#" onclick = removefromCart("' . $item . '", "' . $nonce . '", ' . $deductQty . ');></a>';

And also used this :

$output .= '<a title="Minus this" href="#" onclick = "removefromCart("' . $item . '", "' . $nonce . '", ' . $deductQty . ')"></a>';

But in both cases there is Uncaught SyntaxError: Unexpected token }

desiDesign
  • 41
  • 7

3 Answers3

0

You should use single quotations to represent string in HTML element

$output .= '<a title="Minus this" href="#" onclick = "removefromCart(\'' . $item . '\', \'' . $nonce . '\', ' . $deductQty . ')"></a>';
Thum Choon Tat
  • 3,084
  • 1
  • 22
  • 24
0

The quotes is totally wrong. Do this way, using the first one:

$output .= '<a title="Minus this" href="#" onclick=\'removefromCart("' . $item . '", "' . $nonce . '", ' . $deductQty . ');\'></a>';

See the 's I have added and removed spaces? And please, next time, don't mingle both PHP and JavaScript. It's confusingly dangerous.

See also:

Community
  • 1
  • 1
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
0

For greater than or equal to PHP5

$output .= '<a title="Minus this" href="#" onclick = removefromCart($item,$nonce,$deductQty);></a>';

For Less than PHP5 try it

$output .= '<a title="Minus this" href="#" onclick = removefromCart(' . $item . ',' . $nonce . ', ' . $deductQty . ');></a>';
Hikmat Sijapati
  • 6,869
  • 1
  • 9
  • 19
  • 2nd one comes like this in inspect : `onclick="removefromCart(9452902fb2661d341204550e807a1442,6c9864b056,"` – desiDesign Dec 09 '16 at 07:03