SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2006
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript and PHP question

    I'm trying to make an onClick button that executes a PHP function. This is the code I have:

    Code:
    <form>
    <input type="button" value="refresh"
    onClick="<?php refresh($q, $tmp, 00000); ?>">
    </form>
    What I've found is that whether or not I click the button the PHP function is called. Is it not possible to do this? How can get this to work?

  2. #2
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP is server side language, javascript a client side language. Mixing them up like this is not possible. You can get them to work nicely together, either by posting the form and sending a result page or by use the xmlHttpRequest object... more commonly known as using ajax.

  3. #3
    SitePoint Zealot
    Join Date
    Sep 2006
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was afraid you were going to say that. Ah well. Time to learn ajax.

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can certainly mix js and php, but you have to realize the way things work:

    1) A browser requests a page from the server.
    2) If the page has a php extension, the server sends the page to php.
    3) The php engine executes all the php code and removes ALL the php from the page. The result is a page containing only html and js.
    4) The server sends the page to the browser.
    5) The browser renders the page.

    Therefore, on the server side, the php in this line:

    onClick="<?php refresh($q, $tmp, 00000); ?>">

    is executed, and all function calls are replaced by their return values. So, on the server, that line is turned into something like:

    onClick = "someVal"

    and then the page is sent to the browser.

    By the way, you should use onclick instead of onClick. onClick works in html because html is case insensitive. But js is case sensitive, so if you use onClick in pure js code(i.e. not inside an html element), then the js code won't know what you are referring to.
    Last edited by 7stud; Mar 1, 2007 at 03:26.

  5. #5
    SitePoint Zealot
    Join Date
    Sep 2006
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, that's what I suspected was happening. Since I don't know javascript I wanted to confirm it. Thanks.


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
  •