0

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!

3
  • 1
    Are you viewing the source? A <script> tag doesn't result in any rendered output. Commented Dec 30, 2014 at 17:24
  • 1
    You also want to make sure the element information actually exists in the DOM first before trying to modify it. If you have this script at the top of your page, it's likely not ready in time. Commented Dec 30, 2014 at 17:26
  • 1
    Don't concatenate data directly into JavaScript, use json_encode() which is compatible with JS. Don't echo a new script tag every time, there is no reason for that. It looks like you're trying to update something as you go... and for that you're going about this all wrong. Use AJAX or something so that the page client-side is actually there and can get the status information from the server. Commented Dec 30, 2014 at 17:28

3 Answers 3

1

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> 
Sign up to request clarification or add additional context in comments.

1 Comment

When writing code to pass data between layers, you should always do it transparently. Use json_encode() in your PHP block. Perhaps some day $num is something else that you don't expect it to be today. Also, if you're not setting HTML then don't use innerHTML. Assign a text value instead.
0

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.

Comments

0

Escape the / in </script>.

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

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.