How can I implement this:
<?php
echo "<script type='text/javascript'>";
echo "document.getElementById('table_apps').innerHTML = json_encode($td_temp);";
echo "</script>";
?>
There is a problem with $td_temp;
-
Have you set $td_temp with some text or something? You can use isset to checkJustin Mitchell– Justin Mitchell2014年04月19日 06:01:20 +00:00Commented Apr 19, 2014 at 6:01
-
Yes there is a string inside of the $td_temp to draw a table. I checked it. That's OK.Sponge Bob– Sponge Bob2014年04月19日 06:03:45 +00:00Commented Apr 19, 2014 at 6:03
-
It's also because json_encode is a php function, so you need to remove it from the string eg. " . json_encodeJustin Mitchell– Justin Mitchell2014年04月19日 06:05:43 +00:00Commented Apr 19, 2014 at 6:05
-
I've added an answer that I think you'll find useful from an informational/learning perspectiveJustin Mitchell– Justin Mitchell2014年04月19日 10:09:25 +00:00Commented Apr 19, 2014 at 10:09
5 Answers 5
json_encode() is a PHP function
Do like this
echo "document.getElementById('table_apps').innerHTML ='".json_encode($td_temp)."';";
5 Comments
you can try this without echo
<?php
// some code
?>
<script type='text/javascript'>
document.getElementById('table_apps').innerHTML = '<?php echo json_encode($td_temp);?>';
</script>
<?php
// some code
?>
UPDATE 2 :
<?php
echo "<script type='text/javascript'>";
echo "document.getElementById('table_apps').innerHTML = '".json_encode($td_temp)."';";
echo "</script>";
?>
1 Comment
You should better print not all this html from php, but only a variable. Looks more neat
?>
<script type='text/javascript'>
document.getElementById('table_apps').innerHTML = <?php echo json_encode($td_temp); ?>;
</script>
<?php
7 Comments
<?php
echo "<script type='text/javascript'>";
echo "document.getElementById('table_apps').innerHTML = '".json_encode($td_temp)."';";
echo "</script>";
?>
Try this.
4 Comments
It's vitally important to:
- encapsulate the encoded json string in quotes
- escape any encoded json string
Why?
Because if you were to provide an array of names for example, and you had O'Connell or any other name that has a quotation mark in it, then the innerHTML = '<?php echo json_encode...' would break because of an un-escaped quotation mark. It's important to practise proper encapsulation and validation as much as possible because it future proofs any application you develop, reduces points of failure, or in the case of try/catch/exception, clearly defines intended points of failure.
What happens if you were to create a json object that had a semi-colon in the content? Unless you're wrapping your json content in single quotes and correctly escaping it, it might be interpreted literally as the termination of the current command. So when you try to enter .innerHTML='{blah:"something O'Connel;...}', your Javascript will throw an error because of bad formatting, not to mention it can't interpret 'Connel' before the terminating character. Some browsers are more slack that others (IE I'm looking at you) and will either skip validation or perform a sub-standard routine, or in the case of IE6-8, perform the wrong validation.
There have been mostly correct and incorrect answers so far, but so far all answers have neglected to do one thing, escape quotes:
<?php
echo "<script type='text/javascript'>";
echo "document.getElementById('table_apps').innerHTML = '" . addslashes(json_encode($td_temp)) . "';";
echo "</script>";
?>
That will correctly:
- Escape quotes, and
- Generate a json-encoded object
2 Comments
php > $a = array("apples" => "o'connel"); php > print json_encode($a); {"apples":"o'connel"} Notice how it doesn't add slashes? You can also be selective about the addslashes function, I suggest you read up on it. How do I know? Well, it's because I've developed a number of REST applications and ajax driven web applications using JSON as the message content service.