SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Calling javascript functions in the middle of a page?

    I've got this javascript function called addValue(value). All it does is add some innerHTML to a <div>. If I call this function in my window.onpageload, I see the values in the div. However, I want to call the function in the middle of my PHP page, after I get an array of values from MySQL. So basically instead of calling the function in window.onpageload, I want to do it like this:

    //include javascript functions
    //window.onpageload stuff

    //make some mysql db queries and get some values

    //foreach value
    //addValue(value)

    //show the <div> and see the values

    Except this doesn't work. I know the function works though because if I call addValue from within window.onpageload, it works and I see the values - only at that point the entries are not pulled from MySQL...plus the function is inside window.onpageload.

    How do I do this?

    Thanks

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can't. You have a fundamental misunderstanding of when php code executes and when js code executes. Just because they are on the same physical page doesn't mean php and js execute at the same time.

  3. #3
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To expand on 7's comments, JavaScript executes at the client, PHP at the server. Never the twain shall meet.

    On the other hand, AJAX (a term 7 is known to disdain) may offer some relief in that it can receive data on the fly and, with JavaScript manipulating the DOM, can update the page accordingly. Also, an iframe may offer some options in regard to your situation.

    Mister stud is correct, however, in regard to a standard page: once it's sent from the server, it's content is basically fixed and while JavaScript can work locally, it cannot query a database, etc. (again, AJAX [sorry, 7] is the exception, hence the buzz around it.)

  4. #4
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not just do what you're doing in JS in PHP before it writes the page? If you show us some code I might be able to offer a better detailed suggestion.


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
  •