Reputation: 26281
I need to generate the following script using PHP and json_encode().
var obj= {
o1:123,
o2: {
o3: 456,
o4: function() {return $( "#myID" ).val();}
}
}
My attempt to do so is as follows.
<?php
$a=array(
'o1'=>123,
'o2'=>array(
'o3'=>456,
'o4'=>'function() {return $( "#myID" ).val();}'
)
);
$json=json_encode($a);
?>
<script type="text/javascript">
<?php echo("var obj={$json};");?>
console.log(obj);
</script>
The resultant output is as follows. The quotes around the properties poses no issues, however, the quotes around the JavaScript renders it as a string and not JavaScript. I obviously can't not quote the JavaScript in the array as it will result in malformed JSON.
How can I include JavaScript in PHP's json_encode()?
var obj={
"o1":123,
"o2":{
"o3":456,
"o4":"function() {return $( \"#username\" ).val();}"
}
};
Upvotes: 0
Views: 266
Reputation: 190952
JSON does not support functions. It's a data interchange format. Perhaps you should send some configuration data down and have a function change its behavior on that. Or since you aren't doing it as an AJAX call at this point, just echo it out:
<script type="text/javascript">
<?php echo("var obj={$json};");?>
<?php echo("obj.myFunc = function..."); ?>
console.log(obj);
</script>
Upvotes: 2
Reputation: 10975
How about removing the quotations that surround the function?
<?php
$obj = array(
'o1' => 123,
'o2' => array(
'o3' => 456,
'o4' => 'function() {return $( "#myID" ).val();}',
'o5' => 'function(param) {return $( "#myID" ).val();}'
)
);
$json = json_encode($obj);
while ($func = strpos($json, '":"function(')) {
$json = substr($json, 0, $func + 2) . substr($json, $func + 3);
while ($quote = strpos($json, '"', $func + 2)) {
$func = $quote + 1;
if (substr($json, $quote - 1, 1) == "\\") {
$json = substr($json, 0, $quote - 1) . substr($json, $quote);
continue;
}
$json = substr($json, 0, $quote) . substr($json, $quote + 1);
break;
}
}
echo $json;
This checks if the string starts with function(
, and if it is, removes the double quotes.
The result is a JSON (but can still be used as a JavaScript object):
{"o1":123,"o2":{"o3":456,"o4":function() {return $( "#myID" ).val();},"o5":function(param) {return $( "#myID" ).val();}}}
Upon setting this object to a variable, you can see that the function registered fine.
With that, you can still use the same technique you used before:
<script type="text/javascript">
<?php echo("var obj={$json};");?>
console.log(obj);
</script>
Upvotes: 2
Reputation: 26281
This is what I ended up doing. Kind of hackish, and I expect Dave's is a better solution, but haven't yet tested it. I will do so.
<?php
class fubar {
public function json_encode_JS($array)
{
$array=$this->_json_encode_JS($array);
$json=json_encode($array);
$json=str_replace(array('"%','%"'), '', $json);
$json=str_replace('\"', '"', $json);
return $json;
}
private function _json_encode_JS($array)
{
foreach($array as $key => $value){
if(is_array($value)){$array[$key]=$this->_json_encode_JS($value);}
else {
if(strpos($value, 'function(')===0){
$array[$key]='%'.$value.'%';
}
}
}
return $array;
}
}
$array=array(
'o1'=>123,
'o2'=>'function() {return $("#myID").val();}',
'o3'=>array(
'o4'=>456,
'o5'=>'function() {alert("Hello why don\'t you answer "+$("#myID").val());}'
)
);
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
<?php
$obj=new fubar();
echo('var obj='.$obj->json_encode_JS($array).';');
?>
console.log(obj);
obj.o3.o5();
</script>
<input id="myID" name="myID" type="text" value="hello">
Upvotes: 1