-1

I'm trying to implement a javascript for loop in which a php variable is counting (phpVar++) each loop. However, the variable, which starts at 0, always ends up being 1, even though the loop loops multiple times.

Is this not possible?

<script>
    <?php  $totalMarkers=0; ?>
    for (var i = 0; i < markers.length; i++) {
      <?php $totalMarkers=$totalMarkers+1; ?>
    }
    <?php echo $totalMarkers ?>   //this always prints "1"
</script>

I'm trying to do this so that I can print $totalMarkers in different places in the Body of the HTML.

4
  • Duplicate of so many questions that I've lost count. Commented Aug 20, 2012 at 18:21
  • 1
    PHP/JavaScript doesn't work that way. You can't do that. Commented Aug 20, 2012 at 18:21
  • @SimonAndréForsberg I did multiple searches before posting and didn't find anything. Commented Aug 20, 2012 at 18:22
  • @swl1020 Here's one example: stackoverflow.com/questions/7100633/… . Google for "change php variable in javascript" or similar to find more. Commented Aug 20, 2012 at 18:26

6 Answers 6

7

Javascript is executed on the client computer, PHP is executed on the server. So, your loop does not run until the page is completely loaded in the user's browser -- at that point, no PHP will be executed.

When that page is rendered, this is the process:

<script> <-- gets output literally

<?php $totalMarkers=0; ?> <-- has no output

for (var i = 0; i < markers.length; i++) { <-- is output literally

<?php $totalMarkers=$totalMarkers+1; ?> <-- has no output

} < -- is output literally

<?php echo $totalMarkers ?> <-- outputs 1

</script> <-- is output literally

If you were to view source, you would see this:

<script>
    for (var i = 0; i < markers.length; i++) {
    }
    1   //this always prints "1"
</script>
Sign up to request clarification or add additional context in comments.

Comments

5

PHP runs on the server. JavaScript runs in the browser.

Your server runs the PHP, and will then send this to your browser:

<script>
        for (var i = 0; i < markers.length; i++) {
      }
    1</script>

The only way JavaScript can affect PHP is with AJAX or similar.

Comments

3

PHP runs server-side, JavaScript runs client-side (ignore Node.JS et.al.)

The HTML your browser receives will look like:

<script>
    for (var i = 0; i < markers.length; i++) {
    }
    1   //this always prints "1"
</script>

So, I'm not sure what you want to accomplish here, but you have to keep in mind that PHP and JavaScript can not interact directly.

Comments

2

You cannot mix PHP and JavaScript like that. It doesn't work that way.

The PHP is ran before the JavaScript is. So, the lines:

  • <?php $totalMarkers=0; ?>
  • <?php $totalMarkers=$totalMarkers+1; ?>
  • <?php echo $totalMarkers ?>

are ran before your browser sees it.

Your browser just sees:

for (var i = 0; i < markers.length; i++) {
}
1

Comments

1

No, it is not possible - not without using a bunch of AJAX which seems totally inappropriate for you. PHP is executed way before any JavaScript can be executed. PHP is Server-sided, JavaScript is client-sided. Thus, JavaScript can not control PHP.

Comments

1

You can't do that. PHP is a Hypertext Pre Processor. It is executed before any Javascript code. The code you actually have takes $totalMarkers and initializes it to 0. Then it adds one and then it echoes... It will always be "1" !

You need to count using a Javascript variable then send it to the server if you want to manipulate it with PHP.

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.