EmWe
EmWe

Reputation: 189

Including and using PHP-code that comes from JSON response

I'm using a websocket server that sends data (in this example $html) via json_encode to my client. (index.php) The incoming data is appended via jQuery to the content div.

So basically what I need is to echo the php variable that comes from the websocket server in my index.php.

In other words:

How can I include a json response to my code as php code that can be used in other context in this file?

(Really hard to explain this issue)

<!-- index.php -->
<?php
$usersname = 'John';
?>

<div id="content"></div>

<!-- server.php -->
<?php 
ob_start();
include 'file.php';
$html = ob_get_clean();
?>

<!-- file.php -->
<h1>Hello <?php echo $username ?></h1>

Upvotes: 0

Views: 42

Answers (1)

Styphon
Styphon

Reputation: 10447

You can't mix PHP and JavaScript. PHP is server side and only runs on the server. JavaScript is client side and only runs on the client. To get a PHP variable and output it to the page using JavaScript you need to use Ajax.

Upvotes: 1

Related Questions