0

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>

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

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.