Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I am currently trying to pass in PHP array to a javascript Function through onload();

In my SimilarDomains.php:

<?php
$domainsJS = json_encode($similarDomainsUnique);
?>
<body onload="init(<?php echo "\"$domainsJS\""; ?>);">

I do this to pass it as a string object in order to later process the string using JSON.parse(). In the javascript i have

var obj =  JSON.parse(domainsJS);

for string processing. But it seems like I have a SyntaxError: syntax error @ line 1. This is the HTML Doctype. If I remove the doctype, it just goes to the next first line. it only appears when I have the body onload calling php as I did.

How can I process this php array in order to be used in JavaScript. After all this is said and done, I then have to input the processed values into a JS array.

Here is what the body onload turns out to be in the HTML

<body onload="init("{"0":"estatelawyer.com","1":"reaestatelawyer.com","2":"estately.com","3":"thestate.com","4":"estaterescue.com","5":"boisestate.edu","10":"99acres.com","11":"1point3acres.com","14":"green-acres.com","22":"backcountry.com","24":"baby-kingdom.com","25":"landattorney.com","27":"siteground.com","28":"247realmedia.com","30":"siteground.biz","31":"arealme.com","32":"farming-simulator.com","33":"amkingdom.com","34":"searchengineland.com","35":"shoretelsky.com","36":"grantland.com","38":"amsoil.com","40":"lostrealm.ca","41":"kingdomofloathing.com","42":"shorewest.com","44":"domaintools.com","45":"domain.com.au","46":"realmadridstream.net","47":"farming2015mods.com","48":"travelandleisure.com","49":"landofnod.com","51":"bringmesports.com","52":"cricketcountry.com","53":"bringthebaconhome.com\/user\/dashboard","54":"ollando.com","55":"domain.com","57":"travelandlearntrips.com","58":"scarffruit.country","59":"78land.com","92":"propertylawyer.com","93":"propertylawyergroup.com","94":"propertyattorney.com","95":"rocketlawyer.com"}");">
share|improve this question

2 Answers 2

You should just need to echo it straight up without any extra quotes because it's a JSON object

<body onload="init(<?php echo $domainsJS ?>);">
share|improve this answer
    
And use single quotes in the json object since he's using double for the onload attribute – developerwjk Apr 14 at 22:17

Add the json to a var in the JS

echo <<<EOT
<script type="text/javascript">//<![CDATA[
var jsn = " . json_encode($similarDomainsUnique);
var obj =  JSON.parse(domainsJS);
//]]>
</script>
EOT;

This is poor coding:

<?php
$domainsJS = json_encode($similarDomainsUnique);
?>
<body onload="init(<?php echo "\"$domainsJS\""; ?>);">

There is no reason to be jumping back and forth from PHP mode to HTML mode.
There is PHP overhead to each time you change modes.

Below is the basic proper way to create an HTML page.

I like to get the HTML on its way to the Browser ASAP. That is why I flush the output buffer somewhere just after the <body> tag and the Browser will have a few things to do preparing for Start Render.

To accomplish your passing json to <javascript> I assign the value to $js then embed $js in the `

And PHP is never switched to HTML mode.

<?php ob_start("ob_gzhandler");
header('Content-Type: text/html; charset=utf-8');
header('Connection: Keep-Alive');
header('Keep-Alive: timeout=50, max=100');
header('Cache-Control: max-age=3600');
echo <<<EOT
<!DOCTYPE html>
<html lang="en">
<head><title>Sample</title>
<style>
body{font:400 1emArial,sans-serif;color: #f00 ;}
#page{width:100%;background:#ff0;border:solid .5em #000;padding:2em;}
#contents{max-width:50em;background:#00f;margin:0 auto 0;height:10em;color:#ff0;padding:1em;}
h1{color:#000;text-align:center;}
</style></head><body><div id="page">
EOT;
ob_flush();
$js = "\nvar jsn = '" . json_encode($similarDomainsUnique) . "';\n" ;
echo <<<EOT
<h1>Headline</h1>
<p>Paragraph</p>
</div></div></body>
</html>
<script type="text/javascript">//<![CDATA[
function init(){
  $js
  var obj =  JSON.parse(jsn);
}
window.onload = init;
//]]>
</script>
EOT;
ob_end_flush();
?>

If you need obj to be use by other functions:

<script type="text/javascript">//<![CDATA[
var obj =  '';
function init(){
  $js
  obj =  JSON.parse(jsn);
}
window.onload = init;
//]]>
</script>
EOT;
ob_end_flush();
?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.