0

I am very new to PHP and JavaScript. I am currently using echo in PHP to run some JavaScript on the page. I need to make a new javascript array and a new variable that are equal to an existing PHP array and variable, so I did this:

var messages = <?php print_r($messages)?>
var list = <?php echo $message['user_name'].': '.$message['text'].' ('.date('d/m/Y H:i:s', $message['date']).')'.'<hr />'; ?>

However, there is a problem caused by my using echo to echo script containing echo. How would I solve this. I would like to echo it because it is only about 4 lines long, so is there an alternative.

Thank you in advance.

Edit: This is the whole JavaScript. It is for a messaging system. $messages is declared from another PHP function, and the basic aim of this code is to 'refresh' the echo every few seconds so that the user can see new messages without having to refresh their page:

    echo '<script type="text/javascript">;';
    echo 'var messages = <?php print_r($messages)?';
    echo 'var list = <?php echo $message['user_name'].': '.$message['text'].' ('.date('d/m/Y H:i:s', $message['date']).')'.'<hr />'; ?>';
    echo 'setInterval(function(){console.log("hello")},3000);';
    echo '</script>';
user3315726
  • 13
  • 1
  • 5
  • again? this question is asked many times. Search Google / Stack Overflow! – Raptor Feb 19 '14 at 07:32
  • possible duplicate of [Reference: Why does the PHP (or other server side) code in my Javascript not work?](http://stackoverflow.com/questions/13840429/reference-why-does-the-php-or-other-server-side-code-in-my-javascript-not-wor) – Raptor Feb 19 '14 at 07:32
  • I did but I couldn't find a similar question – user3315726 Feb 19 '14 at 07:33
  • From what I can see, that doesn't really solve the double echo problem, it just explains the difference between code executed on the server and code executed on the client – user3315726 Feb 19 '14 at 07:35
  • yes, it's what you don't understand. – Raptor Feb 19 '14 at 07:58
  • I do understand that PHP is executed first on the server, sending HTML/JavaScript to the client, which is why I echoed the javascript, so it could be executed by the client! – user3315726 Feb 19 '14 at 08:37
  • when you echo javascript, PHP codes won't get executed. PHP codes are executed when page load. JavaScript cannot control when PHP is loaded, except you use AJAX calls. – Raptor Feb 19 '14 at 08:39
  • Yes, so how would you recommend setting up the javascript array and variable with PHP? – user3315726 Feb 19 '14 at 08:51
  • Read AJAX tutorials. Recommend to use jQuery for convenience. – Raptor Feb 19 '14 at 09:14

2 Answers2

1

Not getting what you want,but if you want to use php array in javascript than make it javascript array

<script>
    <?php $test_arr = array('apple','banana','mango');?>
    var js_array = <?php echo json_encode($test_arr);?>;
</script>

output

<script>
    var js_array = ["apple","banana","mango"];
</script>
rajesh kakawat
  • 10,330
  • 1
  • 18
  • 38
-1

Your Javascript will execute on the client, not on the server. This means that foo is not evaluated on the server side and therefore its value can't be written to a file on the server.

The best way to think about this process is as if you're generating a text file dynamically. The text you're generating only becomes executable code once the browser interprets it. Only what you place between <?php tags is evaluated on the server.

By the way, making a habit of embedding random pieces of PHP logic in HTML or Javascript can lead to seriously convoluted code. I speak from painful experience.

halfer
  • 18,701
  • 13
  • 79
  • 158
Jalil
  • 26
  • 1
  • 8
  • This answer is a [direct duplicate of this](http://stackoverflow.com/a/13841203/472495). – halfer Apr 26 '14 at 16:15
  • Hi there. As per my note on one of your other post, we prefer copied work to be attributed, so that the OP here can thank the author - often via an upvote. However it is even better, in my opinion, to just add a link under the question. I believe you need 50 rep to do that, but if you were so inclined, you can get that by answering just a few questions. – halfer Apr 26 '14 at 16:29