My PHP code involves encoding an array using json_encode.
$params = array(1=>'something','2'=>'two');
While the encoding process itself works fine, I encountered an issue when trying to embed the JSON data into an anchor tag. The presence of double quotes in the encoded data was causing conflicts with the HTML attributes.
<a class="btn ajax" data-method="test" data-params="{"one":"something","2":"two"}" href="#">test ajax link</a>
The second double quote within the "data-params" attribute was particularly problematic as it disrupted the link functionality.
To address this, I attempted to convert the string from double to single quotes for parsing in JavaScript:
var string = {'one':'something','2':'two'} ;
However, my attempt to use JSON.parse on this string failed. I tried:
var jsonString = dataParams.replace('\'', '"');
But this only replaced the first single quote and did not resolve the issue completely. Any suggestions on how to overcome this challenge?