#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Norcross, GA
    Posts
    458
    Rep Power
    15

    Merging PHP with Javascript


    Code:
    <javascript>
       if $something == true {
           alert ($something);
           <?
                  echo "howdy"
            ?>
       }
    </script>

    Question: Will php only process where $something=true, or will it print "howdy" whether $something=true or $something=false?
  2. #2
  3. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    793
    It will always print "Howdy" because it is done on the server side. It doesn't care about what is js code.
  4. #3
  5. No Profile Picture
    Not there when you need me
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Oct 2001
    Location
    Berlin, Germany
    Posts
    1,430
    Rep Power
    15
    First, PHP will echo "howdy", then the page will get to the client and you'l get a syntax error because your JavaScript is wrong (no $ in var names and if condition in () ). Anyway, I don't think you can start the script code with <javascript> (afaik no such tag, btw) and close it with </script> (start/end tags must match).
  6. #4
  7. Web Developer
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2001
    Location
    Finland
    Posts
    719
    Rep Power
    16
    When PHP and Javascript merge, they will do it in that order. Javascript has no impact on how PHP acts. After the page has been loaded, your example will look like this:
    Code:
    <javascript>
       if $something == true {
           alert ($something);
           howdy
       }
    </script>
    Note also that you have a slight parse error in the PHP line
    Anyway, Javascript cannot affect PHP. It's PHP that affects Javascript. This is due to the nature of server-side and client-side. Client-side is executed your computer (by your browser) and server-side is executed in the server that your browser contacts to find a specific file. So to convert your example to the right syntax, it would look like this:
    Code:
    <?php
    if($something)
      echo '<script>alert($something);</script>';
    ?>
    Last edited by Datamike; January 4th, 2002 at 09:13 AM.
    -- Tomi Kaistila
    -- Developer's Journal

    The more you learn, the more you know.
    The more you know, the more you forget.
    The more you forget, the less you know.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Norcross, GA
    Posts
    458
    Rep Power
    15
    Thanks fellas. I was lazy, I went ahead and mocked up a test page and confirmed your results.

    Back to the drawing board (ie, converting a somewhat involved javascript to php).
  10. #6
  11. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Oct 2001
    Location
    New Zealand
    Posts
    1,774
    Rep Power
    25
    You can merge the two by doing:

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

    if (something == true) {
    document.write("howdy")
    }
    </script>

    Or something along that vein.

IMN logo majestic logo threadwatch logo seochat tools logo