Grow your CSS skills. Land your dream job.

Javascript swap image doesn’t work inside PHP in Chrome browser

  • # January 17, 2013 at 9:42 pm

    < ?php
    mysql_connect(“localhost”,”root”,”") or die (mysql_error());
    mysql_select_db(“motoactiv-db”) or die (mysql_error());

    $sql = “Select * FROM products”;
    $result = mysql_query($sql);
    while($record=mysql_fetch_array($result))
    {
    ?>

    < ?php
    }
    ?>

    The whole time I was testing this was with Chrome, I thought that there’s something wrong with the markup but when I tried it with Firefox and IE, it works just fine. I also tried eliminating the PHP markup and leave it as HTML and replaced the sources with the image path, it works with Chrome but when I try to put it inside PHP, it doesn’t. What could be happening with chrome? Is it not compatible with javascript running inside PHP? Is that possible? Coz if it is then I think I should look for another write up about swap image, I really need to use this with PHP because i will be using a lot of images here. Please help. :(

    # January 18, 2013 at 1:26 am

    This is more likely a caching issue. Clear your browser cache in Chrome and see if it works.

    > Is it not compatible with javascript running inside PHP? Is that possible?

    No, it’s not possible. Here’s the thing to understand:

    (1) PHP happens *first*, **on the server**. Your script’s output *does not vary** across browsers.

    **unless, of course, you intentionally detect the browser and serve different content.*

    (2) JavaScript happens *later*, **on the client’s computer**. It *does not* run “inside” PHP (in fact, it has absolutely no idea that PHP even exists).

    *****

    Now, it *is* possible that Chrome and Firefox/IE are treating the same output differently. This is something you’ll be able to check without involving PHP: get your javascript working cross-browser first, then write your PHP to output the cross-browser version afterwards.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".