SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2011
    Location
    Bellingham, WA
    Posts
    450
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    if/then with both php and Javascript

    Hello!

    Is it possible to combine an if/then statement with javascript and php? With php we can "combine" php and html by just ending php such as:

    PHP Code:
    <?php if ($is_it_true){?>
    Code HTML4Strict:
    <strong>HTML STUFF</strong>
    PHP Code:
    <?php ;} ?>
    Is there an analogous way to combine javascript and php?

    Basically, I have an if/then statement that checks something from the server AND needs to check out something on the page..in other words, something like:

    PHP stuff: IF ($user_type !="instructor") && Javascript stuff: (getElementByID("nyroModal)) then back to PHP to do some other stuff.

    Thank you,

    Eric

  2. #2
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Location
    Dartmouth, MA, USA
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could do it only if the JavaScript file was being output dynamically by PHP (which it can do) and that execution never returns to the PHP. For example, you could write a certain line of JavaScript conditionally based on a test in the PHP.

    However, JavaScript cannot affect the running of PHP code because PHP is executed on the server, while JavaScript is executed by the client. PHP constructs a web page consisting of HTML, CSS and JavaScript, and once it's done running, sends the output to the client. The JavaScript doesn't run until it gets to the client, meaning there's no way for the JavaScript to pass control back to the PHP code in the way that you've shown above.

    Instead, you could use a part of JavaScript called AJAX, which allows the client to send and receive messages with the server without having to reload the page. This would seem to be what you would want, since you want the JavaScript to be able to call on some PHP code if a certain condition is true.

  3. #3
    SitePoint Evangelist
    Join Date
    Mar 2011
    Location
    Bellingham, WA
    Posts
    450
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    That makes sense. I'll look into Ajax. Thank you for your response!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •