Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2004

    Red face Unanswered: calling javascript from PHP class


    I have a serious problem invoke an external javascript function from a php class. Here I attached a simplifed version what the problem in this newsgroup. After copied the file to your server, rename the file to *.tgz and untar the files into your desired unix directory, just type the following line in the web-browser:

    The treemenu should be displayed in the left div, but it doesn't.
    If you only execute the tree menu, it works:


    I have also put some alert() statements in the Javascript for debugging purpose. If you only execute the left_tree.php script, the debugging message is coming up, but running from main.php is not.

    I am wondering how to invoke javascript from a php class?

    Attached Files Attached Files

  2. #2
    Join Date
    Apr 2005
    Baltimore, MD
    Hmmm...there were a few bugs in your javascript code. I fixed those and have uploaded a "working" ajax request. You were calling the function with the wrong parameters and there were some syntax errors I think.

    BUT...your page still is not going to work. I think you've misunderstood some of the key concepts of using AJAX and also maybe have confused when and how to use javascript vs. php.

    First off, javascript is run on the client side and php is run on the server before the page is ever sent to the client. It's been confused many times before on these forums. A php function cannot be called from someone from within their browser - Javascript can. Of course, using AJAX is a way to make this more transparent.

    The main use of AJAX is to pull content generated from a server-side script and put that content into your webpage without refreshing it. Webpasties has a very good base tutorial. So in the webpasties tutorial, the server-side script just generates the city and state of the zipcode entered. This information is then dissected by the client and placed in the right areas.

    Take a close look at your code. Your left_tree.php page builds a javascript script to generate a menu. What the ajax call is doing is taking the output of "left_tree.php" (which is a bunch of javascript enclosed in <script> tags) and placing that into your div "mainleft." So now, mainleft contains javascript. Nothing else. There is no menu because all that exists in that div is some <scripts>.

    Hope that makes sense. What you can try to do is add a call to your menu build function within the onStateChange handler in your .js file. I don't know if this will actually work, though. AND, it's generally not a good idea to have <script> tags outside of your <head> tag. What you should do is have the left_menu.php script actually build out the HTML elements rather than create a javascript function that will dynamically generate them.

    I hope this made sense and I understood what you are trying to do correctly. I didn't get too far into your code, but this should help I'd think.

    P.S. - In the files I've uploaded, when you try to reload the menu, the innerHTML of your "destination div" will be alerted. You'll see that it is just javascript.
    Attached Files Attached Files

Posting Permissions

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