0

I am having one onclick event with some paramters with it. But in some cases i am getting double quaotes or space between strings. so getting error of Uncaught SyntaxError: missing ) after argument list or invalid token.

How to overcome with this issue. I tried to use array and trim the variables but it wont work for me.

$stringArray = ['string 1', 'string'2','001'];
$concatenated = trim(implode(',',$stringArray));

E.g: I am getting 3 string like :

 $data1 = 'surgery';
 $data2 = 'Cardio'c';
 $data3 = 'hopsital ab"cc';

<a id='changeState' onclick=editModal(\"". $data1 ."\",\"". $data2 ."\",\"".$data3."\");>Click Here</a>

Please help to resolve this issue.

amit sutar
  • 27
  • 4
  • Where is your PHP code that generates this html line ` – GetSet Mar 02 '22 at 05:05
  • 1
    Just `json_encode()` each string that you are injecting into your inline javascript function paramters. https://stackoverflow.com/questions/23740548/how-do-i-pass-variables-and-data-from-php-to-javascript#:~:text=Implementation%20is%20relatively%20straightforward%3A Ultimately, I don't recommend your inline event listener anyhow. Use `data-` attributes if you must, but pull all of your inline javascript out of your html and into an external script. – mickmackusa Mar 02 '22 at 05:05
  • @GetSet function editModal(data1,data2,data3){ if(confirm("Are you sure you want to change state of this attribute?")){ $.ajax({ type:"POST", url:"url", dataType: "json", data:'data1=' + data1 + '&data2=' + data2 + '&data3=' + data3, success: function(response){ $('#msg').text(response.msg); //success code } }); } else{ return false; } } – amit sutar Mar 02 '22 at 05:26

0 Answers0