Results 1 to 4 of 4

Thread: Can javscript run a php file?

  1. #1
    Join Date
    May 2010
    Location
    Sacramento, CA
    Posts
    91
    Thanks
    23
    Thanked 2 Times in 2 Posts

    Default Can javscript run a php file?

    On the home page i have this code

    Code:
     
    function goodbye(e) {
    	if(!e) e = window.event;
    	//e.cancelBubble is supported by IE - this will kill the bubbling process.
    	e.cancelBubble = true;
    	e.returnValue = 'Are you sure you want to logout?'; //This is displayed on the dialog
    
    	//e.stopPropagation works in Firefox.
    	if (e.stopPropagation) {
    		e.stopPropagation();
    		e.preventDefault();
    	}
    }
    If it's possible i'd like to have the function run a php file..POSSIBLE?

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    That depends upon what you mean by 'run'. But, generally yes.

    I often use AJAX to run PHP files.

    There are at least two issues though for something like this:

    1. What if the user doesn't have javascript?

    2. Your goodbye function might not fire in some browsers.


    Getting back to your question though, What do you want to happen when logout.php is run?

    I mean, does it need to be passed any information, or is everything it needs already available in the SESSION and/or elsewhere?

    Just as an example, I recently was working on displaying PDF on an HTML page. It became desirable to know how many pages there were in any given PDF file on demand. PHP can find this information out fairly easily. But I don't want to load a separate page each time this information is wanted. So I used jQuery and AJAX to get it:

    Code:
    $.get('pages.php?pdf=' + page + '.pdf', function(data){
    	sizeIt(+data);
    });
    This passed the name of the PDF to a PHP page which calculates how many pages are in the PDF. By using AJAX, this is done in the background. Once the server fetches the page, the information is returned to my sizeIt() function. All this without having to leave the current page.

    For what you are talking about you might want the logout page to be loaded for the user. In which case you could just do something like (using only ordinary javascript):

    Code:
    location.href = 'logout.php';
    In any case, your function doesn't appear to actually do anything. How are you using it and what does it do? If it's used as onbeforeunload, there is no way I know of to determine the user's choice, and therefore no way to decide whether or not to launch the logout page.
    Last edited by jscheuer1; 09-07-2010 at 10:32 AM. Reason: add info
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    you can have a ajax call, a php file into a div. Then inside the php file you can have code which would "run"

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by ggalan View Post
    you can have a ajax call, a php file into a div. Then inside the php file you can have code which would "run"
    Yes, but if all you want to do is run the PHP code, you don't have to put it into a div.

    But the real trouble here is that the onbeforeunload isn't a normal event. Because of this we cannot get a boolean value that tells us whether or not the user really wants to log out.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

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
  •