www.webdeveloper.com
Results 1 to 6 of 6

Thread: Call function inside of load()

  1. #1
    Join Date
    Dec 2010
    Posts
    33

    Call function inside of load()

    Hey everyone, long time lurker - first time poster. I'm completely new to Javascript and all of it's clicks and whistles (jQuery, AJAX, etc..). My question is, is it possible to call a function inside of the load event ? I need something to dynamically pull a cookie value each time the function is called. This is what I have:

    Code:
    function populateParentWindow() {
    
         $("#FTPContents").load("functions/testftp.php?HASH="+Get_Cookie("User")+"&reload=<?php echo $_SESSION['currentdirectory'];?>");
         return false; // you'll need this to ensure that you don't actually have a clickthrough to your child window link.
    	 }
    That particular function updates a div after the file has been uploaded. I'm using SWFUpload, I'm sure most of you are familiar with it. I need the HASH to be a cookie value, but the value changes depending on the current directory the user is viewing. Can this be done ?

  2. #2
    Join Date
    Dec 2005
    Posts
    2,984
    First of all there's no such thing as a load event - onload is an event. However, I do know that you are referring to the $.load function in the jQuery library.

    Second, that's pretty much what $.load was designed for, a callback function. Check the documentation here -> http://api.jquery.com/load/

    Briefly, this is how it works:
    Code:
    $('#FTPContents.load(url,dataReturned,function() {
       alert('stuff has been loaded');
    });
    You are mixing php with Javascript, which is a no no, PHP is not javascript and cannot be used like you're using it.

    Good luck.
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...
    awesome.

  3. #3
    Join Date
    Dec 2010
    Posts
    33
    Well, the PHP works fine inside the javascript. It's probably not good practice, but it does work. The problem that I'm having is that PHP, as you know, is server-side, where javascript is client-side. That particular function needs to update the value of HASH and reload before it is executed. The example you showed me wont do that. It will simply call the page. I need the page to be requested like this

    test.php?HASH=whatehashisatthetime&reload=whateverpagetoreload

    the hash is stored in a cookie, and so is the reload page.

    Is there anyway to use load() to retreieve the cookies and parse the information where it needs to be in the URL. Thats what I'm trying to accomplish.

  4. #4
    Join Date
    Dec 2005
    Posts
    2,984
    What you just said is the definition of PHP and Javascript "not working".

    You don't need to use load to retrieve cookies, you can just use normal Javascript. And that, fortunately, can be done. Cookies can be set using PHP and read using Javascript and vice versa.

    Check out http://www.w3schools.com/js/js_cookies.asp - they've got great code samples that will come in handy.

    Where does the has come from? You're likely going to have to use AJAX.
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...
    awesome.

  5. #5
    Join Date
    Dec 2010
    Posts
    33
    lol.

    I'm not javascript expert at all, but I can tell you 100% that you can use PHP echo() inside of javascript and it works swimingly, My problem is that I'm converting from using PHP Sessions, to just cookies. Because one thing you cannot do is update a PHP Session variable with javascript, because Sessions are server-side.

    2 things need to happen here.

    I've just about figured it out, I guess I would just like to explain whats going on.

    I'm creating an web-based FTP manager for my customers, we sell Online Backup. In the account homepage I have 2 DIVS.

    1. FTP Manager, which shows testftp.php
    2. SWFUpload - I'm sure you know what that is.

    Now, pretty much all the ftp manager div is:

    <div id="ftmanager"><?php include('testftp.php'); ?></div>

    So, testftp.php shows the directory for the user, and what this whole operation revolves around is not having to reload the entire page when the user clicks a link to move through his or her folders. Simple enough, I can do that.

    BUT, the problem is. The current directory that there in must be stored somehow so that when they upload from the SWFUpload div, so the script knows which folder to upload to, and which folder to display when reloading the div. hence the .load jquery with the HASH and reload values.

    This could all be avoided if I just reloaded the entire page, but that is a strain on the server to continuously pull the session information and information from the DB, so this is must.

    Hopefully I've explained what I'm looking for a little better. The cookie will change once testftp.php is loaded, but the SWFUPload div never refreshes, so by itself it cannot know the cookie values have updated, which is why I need to call the Get_Cookie function before the actual load happens.

  6. #6
    Join Date
    Dec 2005
    Posts
    2,984
    I can tell you with 100&#37; certainty that it doesn't work - not the way you want it to. The only reason it works at all is because you're using the script inline - e.g. hardcoded into the HTML document. If the Javascript src was stored in an external file, then it would not work.

    The reason WHY you think it "works" is because when the page is parsed by the PHP parser, it puts the value of <?php echo $_SESSION['currentdirectory'];?> in where it should be. But you SHOULD not be doing this. The second that file is made an external Javascript file it will break.

    Where does the value of HASH come from? I tried to ask that question in the last post but I mistyped, my mistake.
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...
    awesome.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles