1

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;

4
  • Have you set $td_temp with some text or something? You can use isset to check Commented 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. Commented 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_encode Commented Apr 19, 2014 at 6:05
  • I've added an answer that I think you'll find useful from an informational/learning perspective Commented Apr 19, 2014 at 10:09

5 Answers 5

2

json_encode() is a PHP function

Do like this

echo "document.getElementById('table_apps').innerHTML ='".json_encode($td_temp)."';";
Sign up to request clarification or add additional context in comments.

5 Comments

innerHTML should be wrapped in single quotes as well as escaping any single quotes in the json data.
That's exactly what I want. Thanks.
'".json_encode($td_temp)."' - it's wrong no need for single qoutes
@SpongeBob author you should better use my example, believe me i know what i'm talking about
@Alex, I don't agree, if anything your answer was originally wrong and is still incomplete. You don't encapsulate the encoded json string in quotes, nor escape it. This was accepted as correct because it did at least encapsulate the string which is at least 50% correct. The next best answer was provided by Satish because he provided two clear examples of generating PHP with HTML.
2

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

Escape the json for single quotes and this answer is correct
1

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

Your syntax is wrong and you have <?php echo encapsulated in a string
@Justin Mitchell you sure? check it, it's not wrong
Thanks but I want it in php.
You updated while I was posting my first comment. Also, you need to escape the json and surround it in single quotes.
no i don't need, check it, single quotes adds automatically echo json_encode("sfhafdh");
|
0
    <?php
        echo "<script type='text/javascript'>";
        echo "document.getElementById('table_apps').innerHTML = '".json_encode($td_temp)."';";
        echo "</script>";
    ?>
Try this.

4 Comments

Thanks but It doesn't work. Error: Uncaught SyntaxError: Unexpected token <
That's because the string wasn't quoted or escaped
And because json_encode is still encapsulated inside or a php string and will be interpreted literally
but down voting really discourage a developer's strength. :(
-1

It's vitally important to:

  1. encapsulate the encoded json string in quotes
  2. 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:

  1. Escape quotes, and
  2. Generate a json-encoded object

2 Comments

s"o/me_s\tr var a = "s\"o\/me\_s\\tr"; var b = '\"s\\\"o\\/me\\\_s\\\\tr\"'; the second is your string. check it in firebug console it's not working
PHP Console: 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.