Ok, here is what is happening...in simple terms.

I am loading PHP content into a page using AJAX... but certain elements being loaded from the php need specific JS to work (scriptaculous) and I cant load it befoer hand since I need to know the PHP content for the JS.

PROBLEM: I tried using <script> tags as recommended by everyone online... but the script tags arent being processed for some reason. ( i think because the parsed HTML from the PHP page is inserted into a DIV and not read like normal HTML)

Anywho... I need a way to call JS from the PHP page (like the inline <script> tag) but not having the user click a button or anything.

Any ideas?

    The above makes no sense, I'm afraid. I wonder if perhaps you've misunderstood the way that PHP, Javascript, HTML or AJAX works.

    • PHP executes on the server. Inside PHP, no Javascript is executed. If a PHP script generates HTML containing a script element, that code is sent to the client, not executed on the server.
    • Javascript code executes on the client side. Code included in script elements or via a .js is invoked in the order it appears in the page, regardless of where it appears in the page
    • Javascript functions already defined may be triggered subsequently off events, if event handlers have been added (either by attribute or programmatically)
    • A PHP script which returns data as an AJAX POST (e.g. using XMLHttpRequest) cannot place further Javascript code in the response - it must be in whatever format the client code is expecting it in (even if that is JSON, it should not contain Javascript code).
    • XML documents can't (normally) contain Javascript code

    Mark

      Like Mark, I'm not sure what you're asking but I'll make this comment that might help you.

      When I need Javascript on a page to behave differently based on content that was received from an Ajax call, I do it this way:

      1. Initial page load includes a very generic JS function (let's call it function "do_something")
      2. Make Ajax call
      3. Receive Ajax response
      4. When the routine that initiated the Ajax call determines that the message has been received, I scan the received message and set some JS variable in some way based on the contents of the received message.
      5. I pass that variable to function "do_something" which is a static function that has now been "customized" by the value of this new variable.

        Thank you etully... thats what I had done more or less. And to MarkR, Yes I understand how all these pages work, the only thing I was asking that you answered for sure was:

        "A PHP script which returns data as an AJAX POST (e.g. using XMLHttpRequest) cannot place further Javascript code in the response"

        Here, for anybody who cares, is what I ended up doing...

        setup: this is for a mySQL CMS system, specifically a page for adding a new row... and getting a scriptaculous dropdown of values entered already in the database in a autocompleter when typing in the new field. CONFUSED YET?

        So the ADD page is loaded with AJAX and has an ongetfocus event in each textfield, which calls a JS function already loaded on initial page load (like etully mentioned) which uses function variables to custom make the JS for the dropdowns. Another ajax page gets the field value on change and queries teh DB for matches and returns them in a drop down below the field for selecting. VOILA!

        Sorry if this is rushed, but I figured it out at 5am and just wanted to let people know how I did it...

        Thanks for the help!

        Ben

          Write a Reply...