call php file from within javascript

Discussion in 'PHP' started by dess66, May 1, 2010.

  1. #1
    hi,

    Can someone please help me with this:

    <script type="text/javascript">
    window.onbeforeunload = function (evt) {
    if (typeof evt == 'undefined') {
    evt = window.event;
    }
    if (evt) {
    <a href="http://www.1stacademy.net/hello.php">
    }
    return;
    }
    </script>

    I have inserted the code above into a html page so the javascript gets called when the user leaves my web page. what I want to be able to do is execute the file "hello.php" when someone leaves the page. Can anyone tell me what I would replace the line <a href="http://www.1stacademy.net/hello.php" with to do this?

    Thanks
    Derek
     
    dess66, May 1, 2010 IP
  2. n3r0x

    n3r0x Well-Known Member

    Messages:
    257
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    120
    #2
    switch

    
    <a href="http://www.1stacademy.net/hello.php">
    
    Code (markup):
    with

    
    window.location='http://www.1stacademy.net/hello.php';
    
    Code (markup):

    this will redirect the user to hello.php

    OR

    
    window.open ("http://www.1stacademy.net/hello.php","mywindow"); 
    
    Code (markup):
    However this method will be blocked by all pop-up blockers.

    Another way is to use Ajax => http://www.w3schools.com/ajax/default.asp

    Pick one.. :cool:
     
    n3r0x, May 1, 2010 IP
  3. dess66

    dess66 Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for that. Actually I've had some rather strange results with it. I wanted to put my php scripts that aren’t actual pages into a 'php_scripts' directory to keep them separate. however window.location doesn’t seem to like it.

    First of all I found that the only variation that would work is:

    window.location="hello.php"; (worked most of the time???)

    as soon as I changed it to (along with moving the file):

    window.location="php_scripts/hello.php";

    No other variation including the whole address would work. What I am actually trying to do is log the amount of time a webpage remains open for. So I thought if I logged the time in my db using php on the page open I could then use javascript to call another php file when the user leaves the page. the other php file would calculate the time difference and store it in the db.

    Any ideas as to why window.location="php_scripts/hello.php"; doesn’t work? Or how else I might be able to log the durations users stay on my pages? I wondered if it was the browser opening a new page at the time was a problem. Really odd!
     
    dess66, May 1, 2010 IP
  4. n3r0x

    n3r0x Well-Known Member

    Messages:
    257
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    120
    #4
    Can be as simple as the folder "php_scripts" have the wrong rights or the reason it might not work for all can be as simple as the user have Javascript disabled.
     
    n3r0x, May 1, 2010 IP
  5. dess66

    dess66 Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I tried changing file permissions but it didn't make any difference. I don't think there is a problem with the java being ON/OFF. It seems to work OK untill I put the php file in the directory. I also looked at the AJAX link but I am too new to all this to understand that. Is there any other way to do it or are you able to show me it in AJAX?
     
    dess66, May 1, 2010 IP
  6. n3r0x

    n3r0x Well-Known Member

    Messages:
    257
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    120
    #6
    Sure I can write something later..=)
     
    n3r0x, May 1, 2010 IP
  7. dess66

    dess66 Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks, look forward to that.
     
    dess66, May 1, 2010 IP
  8. n3r0x

    n3r0x Well-Known Member

    Messages:
    257
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    120
    #8
    
    <script type="text/javascript">
    function start_ajax() {
    	var temp;
    	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
    		temp=new XMLHttpRequest();
    	} else { // code for IE6, IE5
    		temp=new ActiveXObject("Microsoft.XMLHTTP");
    	}
    	return temp;
    }
    
    function SendRequest(url) {
    	// Create Ajax object
    	var http = start_ajax();
    	// Set how and where it should connect
    	http.open("GET",url,false);
    	// Send the request
    	http.send();
    }
    </script>
    
    Code (markup):
    where the window.location was just call
    
    SendRequest("php_scripts/hello.php");
    
    Code (markup):
    Note: not tested, and for you out there that think it´s looks like w3schools functions.. it´s because it is based on those (could not be arsed to write something from scratch at this hour of the day)
     
    n3r0x, May 1, 2010 IP
  9. dess66

    dess66 Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thanks for taking the time to writing that. I’ve tested it and as annoying as it is, it actually works exactly the same as window.location! I’ve tried the absolute address, as well as just the dir in front but it makes no difference. Calling just hello.php by itself through works fine. Makes me want to go back to basics and assembler…:mad: Could it have anything to do with the fact that I am using it in html pages and not xhtml?
     
    dess66, May 2, 2010 IP
  10. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #10
    Are you using any sessions/cookies or is it simply pure php?
     
    ThePHPMaster, May 2, 2010 IP
  11. s.ham

    s.ham Member

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #11
    Try search for some AJAX libraries.
     
    s.ham, May 2, 2010 IP
  12. dess66

    dess66 Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Just pure php - it's also being used with a mysql database, but I don't know whether the call to the file is failing or if it's the database failing in some way. (I wouldn't know what to look for with AJAX libraries.)
     
    dess66, May 3, 2010 IP