-1

My javascript code appears to work as it's supposed to. However, when I 'view source' in Chrome, it disagrees with the javascript that is actually executed.

Here is my code:

<?php
    $_SESSION['new'] = "blue";
    if (!isset($_SESSION['old'])) { $_SESSION['old'] = "blue"; }
        echo '<script type="text/javascript">
            $(document).ready(function() {
                changeCol("'.$_SESSION["old"].'","'.$_SESSION["new"].'");
            });
          </script>';
    $_SESSION['old'] = "blue";
?>    

$_SESSION['old']="green" from the previous page. The code is supposed to call changeCol("green","blue"), and then set $_SESSION['old']="blue".

In fact, both of these things happen, so my code works as it's designed, but if I view source, it says changeCol("blue","blue"). This is strange, because if in changeCol() I write the passed variables to console.log, I get green, blue.

So if it's calling changeCol(green,blue) why does it say changeCol(blue,blue) when I view source?

Jeff
  • 12,147
  • 10
  • 51
  • 87
  • 3
    js before php ...its not possible .... php is server side and js is client side – NullPoiиteя Dec 07 '12 at 04:25
  • 6
    When you view source, your browser is probably requesting the page a second time, so you get a fresh page with the new session values. –  Dec 07 '12 at 04:26
  • 2
    Did you call session_start()? – blackbourna Dec 07 '12 at 04:27
  • 1
    Whatever is wrong is happening server-side. It's not the result of js execution. – Beetroot-Beetroot Dec 07 '12 at 04:28
  • 1
    @BrianMarshall ah, good call! I checked developer tools in chrome and it's as you'd expect. if you want to answer i'll give you a check. everyone else: yes. i know php executes first, but that doesn't answer my question. my fault for poor wording. – Jeff Dec 07 '12 at 04:31

2 Answers2

12

When you view the source, you're probably making an additional request. Your session variable will be reset.

If you're using Chrome or Firefox — which you should be — you can open up either the Web Developer Tools or Firebug and examine the actual DOM tree. (This is also pretty useful in situations where a script has added content dynamically.)

Waleed Khan
  • 11,426
  • 6
  • 39
  • 70
  • From my experience, this is most definitely the right answer. – Scotty C. Dec 07 '12 at 04:29
  • 1
    View source makes a request? In which browsers? – Beetroot-Beetroot Dec 07 '12 at 04:31
  • @Beetroot-Beetroot chrome, apparently! – Jeff Dec 07 '12 at 04:32
  • Chrome will actually ask you if you want to resubmit the form when you try to view the source. – Waleed Khan Dec 07 '12 at 04:33
  • @WaleedKhan there is no form, at least not in my example? without a form, it doesn't ask--it just does. – Jeff Dec 07 '12 at 04:33
  • 2
    does appear to be a know issue in chromium http://code.google.com/p/chromium/issues/detail?id=523 – Kelly Copley Dec 07 '12 at 04:37
  • 4
    Someone at Google needs to be fired. View source should not make a new request, at least not without use sanction, otherwise there's a real danger that the source you get is not the source of the page you are viewing. The OP's problem is a case in point, as would be anything with rapidly changing dynamic content, eg news or stock prices sites. – Beetroot-Beetroot Dec 07 '12 at 04:45
  • 2
    @Beetroot-Beetroot: The current behavior (once you know it) is actually useful for devs to debug pages with changing content. You don't need to refresh the page and view source, refresh and view source, refresh and view source... you can just refresh the source view. Helped me lots of time to spot bits that change in the source just by pressing F5. Note, that chrome implements view source as a protocol, it's `view-source:some_uri` instead of `http://some_url`. So you can go directly to source without rendering the page. – slebetman Dec 07 '12 at 04:58
  • I concur this is what happens from my experience. – Tanzeel Kazi Dec 07 '12 at 05:09
  • 1
    @slebetman , I can see that F5...F5...F5 might be useful and shouldn't be a problem given that it's under user control. I still demur on View Source making a request on first invocation. That's just plain wrong. – Beetroot-Beetroot Dec 07 '12 at 05:19
  • Besides which, I'm enjoying the 3rd test in Kolkata right now. Cook (162 not out) is going through a bad patch. – Beetroot-Beetroot Dec 07 '12 at 05:25
1

Did you include <?php session_start(); ?> on your second page?

  • 1
    If he didn't, it's amazing how well those $_SESSION variables are working. :P – Scotty C. Dec 07 '12 at 04:29
  • 1
    WIth no session_start, php would make a new array called `$_SESSION`. `$_SESSION['old']` would always be unset and set itself to blue. if this were the case –  Dec 07 '12 at 04:31