简体   繁体   中英

php object passing to javascript function does not work despite using htmlspecialchars

In a php function I receive a resultset "$pRecord" that can be extracted with extract($pRecords).. works fine, te I get variables $name, $description... Now I want to pass this resultset to a javascript function this way:

<img onclick='var myObj = .....; myfunc(myObj)'/>

Clicking the image I get the error:

Uncaught SyntaxError: expected expression, got '}'.

My original code is:

$SEG = "var wObj = JSON.parse('".htmlspecialchars(json_encode($pRecord))."'); ";
$OMCsl = "$SEG Segment.showSegmentlanesBox(wObj);";
$Z .= "<img class='segs' onclick='$OMCsl' src='$this->iconpath/segmentlanes.png' title='id=$id'/>";
echo $Z;

Leaving htmlspecialchars away yields the same error.

What's the problem?

Remove htmlspecialchars and quotes around JSON object.

$SEG = "var wObj = JSON.parse(" . json_encode($pRecord) . "); ";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM