user2966785
user2966785

Reputation: 19

PHP echo with Javascript syntax?

Ok, Apologies because this is a kind of a repeat of some previous questions but somehow those aren't getting my any closer to an answer. I have a while loop in PHP where I'm trying to update the contents of a div using Java script and I think I must be messing up the syntax somehow because I'm not getting any output. Here's the code:

 echo '<script language="javascript">document.getElementById("information").innerHTML = "' . $num . ' files processed.";</script>';

Am I missing something here syntax wise? I feel like I must be because I can echo out just $num and get the correct values. Thanks in advance!

Upvotes: 0

Views: 88

Answers (3)

Rahul Desai
Rahul Desai

Reputation: 15491

Escape the / in </script>.

echo '<script language="javascript">document.getElementById("information").innerHTML = "' . $num . ' files processed.";<\/script>';

Upvotes: 0

Marek
Marek

Reputation: 7433

The output is likely cached on the server side. Use flush() after each echo, turn off any output buffering, gzip compression in webserver. You might also try to output white characters to fill the buffers. And inspect what you get in firebug.

Upvotes: 0

Vitalii
Vitalii

Reputation: 1175

You can do it from javascript side. Just write a simple script in your php file

<script type="text/javascript">
    var num = <?php echo $num ?>;

    document.getElementById("information").innerText = num + "file proceed";
</script> 

Upvotes: 1

Related Questions