I'm trying to run a quick php script when users leave my website, and also pass a variable from my javascript to php but i'm not quite sure how to include the php file and pass it a var. But it's not actually running the php script. any ideas?

(the javascript gets the username from an external activity function, and i know that works i tested the var on an alert and its there.)

My JavaScript:

<script language="javascript" type="text/javascript">

            var username = null;

            function GetUsername(usernameff)
            {
                username = usernameff;
            }

            window.onbeforeunload = function () 
            {

            if (username != null)
            {

            <?php include("scripts/RemoveUserOnDisconnect.php?username=username");?>
            }

            }


            </script>

My RemoveUserOnDisconnect.php File:

<?php
    mysql_connect("mysql.mysql.com", "username", "password");
    mysql_select_db("my_db");
    mysql_query("DELETE FROM my_table WHERE username = '$username'");         
?>
share|improve this question

3 Answers

up vote 4 down vote accepted

Try an ajax request. Depending on your php script you will need $.post or $.get

jQuery:

<script language="javascript" type="text/javascript">

    var username = null;

    function GetUsername(usernameff){
        username = usernameff;
    }

    window.onbeforeunload = function(){
        if (username != null){
          $.post('scripts/RemoveUserOnDisconnect.php', {username: username}, function(){
            //successful ajax request
          }).error(function(){
            alert('error... ohh no!');
          });

        }
    }
 </script>

EDIT:

Your php script should reference the $_POST array if you use my code above.

<?php
    $username = $_POST['username'];

    mysql_connect("mysql.mysql.com", "username", "password");
    mysql_select_db("my_db");
    mysql_query("DELETE FROM my_table WHERE username = '$username'");         
?>
share|improve this answer
Thanks a lot for the examples! I gave it a shot, and have exactly what you posted. No luck though, any ideas? I tried putting a alert("it worked"); in place of your it worked comment, and the alert isn't coming up. Should I put like a return in front of the query to get it to trigger that function or something? to see if it's being executed? Also, I don't know if this could be it, but I'm only allowing "execute" permissions on the scripts folder. But i'm assuming that probably isn't the issue since i have other scripts in the folder being accesed just find through a cron job – brybam Mar 8 '11 at 1:13
are you including the jQuery library? – Kyle Mar 8 '11 at 1:15
ohh I didnt know that was jquery, i thought it was just regular javascript. I have <script type="text/javascript" src="scripts/jquery.js"></script> up higher in the script, I also tried src="scripts/jquery.js" in the script tab that contains this and it still doesnt work – brybam Mar 8 '11 at 1:19
@brybam Sorry, I thought I was clear this was jQuery. In the head section of you HTML document, put this code. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script> Also, make sure the path to your php script is correct and everything should work fine, assuming your php code works. Good luck! – Kyle Mar 8 '11 at 1:23
hmm. I know the php code works, i'm using the same exact code in a php function i reference with flash so i know it's working. I also know it's getting a username varable, because if i take out the code you recommended and put alert(username); it shows up. I thought i'd mention i tried, $.post('scripts/RemoveUserOnDisconnect.php', {username: username}, function(res){ alert("results: " + res); and the alert isn't even coming up – brybam Mar 8 '11 at 1:45
show 7 more comments

Make an ajax call to scripts/RemoveUserOnDisconnect.php?username=username. You cannot include PHP with Javascript, Javascript executes on the browser, PHP executes on the server..

share|improve this answer

You need to make an AJAX call to the PHP script. Your javascript code will just make a background request to the php script which will execute those MySQL statements. Google up AJAX. Recommended: Learn jQuery and use ajax using $.get() or $.post()

share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.