使用锚标记上的on-click函数将PHP对象传递给javascript
问题描述:
Suppose there is a dynamic HTML creation in PHP like the below:
php code
foreach( $content as $obj ){
$displayString .= "<a href='' onclick='return func($obj)'> View Details </a>";
}
echo $displayString;
JSON
$content
being an array of JSON objects
Javascript function:
func()
is a javascript function
How to pass PHP object values into a Javascript function and allow the javascript function parse through an object to display values?
答
If $content is in JSON format then you got to decode it into array
$content = json_decode($content,true);
foreach( $content as $obj ){// $obj is the object
$displayString .= "<a href='' onclick='return func($obj['value'])'> View Details </a>";
}
echo $displayString;
value is the key within the object $obj which you want in the javascript