0

I want to run bellow php page when a user logs in. I'm using bellow php code to show online visits with their name and I also linked their profile page with it.

The php page is running when the online.php is reloading.

It's the normal way of php, I know, but I want to use ajax to run bellow php script. I know how to send form data using ajax, but I don't know how to send data if there aren't any forms inside the page.

How is this possible? I tried it but it's not working yet. Note: I'm also using JQuery.

AJAX code

//update the online users list
$.ajax({
    type: "POST",
    url: "online.php",
    data: data,
    success: function (response) {
        $("#online").html(response);
    }
}); //end it

PHP code

<?php
    session_start();

    include('controller/class/RO_dbconfig.php');

    $sth = $dbconnect->prepare("SELECT first_name,keyId FROM register WHERE status = :online");
    $params = array("online" => 1);
    $sth->execute($params);
    $status = $sth->fetchAll();

    echo "<div id='online'>";

    foreach($status as $onlineArray) {
        echo "<ul class='online'>";
        echo "<li><a href='timeline.php?profileId=".$onlineArray['keyId']."'>".$onlineArray['first_name']."</a></li>";
        echo "</ul>";
    }

    echo "</div>";
?>
Matt
  • 74,352
  • 26
  • 153
  • 180
wasana
  • 21
  • 7

4 Answers4

1

If I understand you correctly, this should help you check if a new user has come online and then fetch your online list with or without POSTed form data.

<script type="text/javascript">
// example variable; you can set this to the ID of the last stored online session in your DB
// the variable will be referenced later to see if any new sessions have been added
var lastConnectionId = 446;

// this function will fetch your online list; if a form has been submitted you can pass the serialized POST data
// else you can pass a null variable
function getOnlineList(data) {
    var requestType = (data == null) ? "GET" : "POST";
    $.ajax({
        type: requestType,
        url: "online.php",
        data: data,
        success: function (responce) {
            $("#online").html(responce);
        }
    });
}

// this function will check to see if any new sessions have been added (i.e. new online users) by comparing to last
// session row ID in the database to the variable lastConnectionId; there must be a table in the database that stores
// sessions and gives them incremental IDs; check-sessions.php should output the latest session ID
function checkSessions() {
    $.ajax({
        type: "GET",
        url: "check-sessions.php",
        success: function (responce) {
            if (responce > lastConnectionId) {
                lastConnectionId = responce;
                getOnlineList(null);
            }
        }
    });
}

// runs checkSessions ever 3 seconds to check for a new user
setInterval("checkSessions", 3000);
</script>

Of course add all your other code and listeners etc. where necessary.

Chrysus
  • 361
  • 1
  • 6
0

You should use load():

$("#online").load("online.php");

Or you could use get (better in your case than post):

$.ajax({
            type: "GET",
            url: "online.php",
            data: {},

            success: function (responce) {

                $("#online").html(responce);

            }



        });
A. Wolff
  • 74,033
  • 9
  • 94
  • 155
0

You cannot make it update when a user com online. You have to poll the online.php with the help of a timer. Execute the code roasted wrote in regular intervals, like described here: Server polling with JavaScript

Community
  • 1
  • 1
bigenius
  • 1
  • 1
0

I'm going to try and rephrase your question, as I'm unsure on what your actual question is.

When a user logs on, you want to update the list of online users. Do you want to update this list on the fly for all currently logged on users? Because that is not (easily) possible using php. You could write a polling script that checks every couple of minutes.

I'm not certain if that is needed. I think it's perfectly okay to refresh this list on each pageload. If you want to push these changes to the users you'll need to look into long polling.

in any case:

AJAX code

setInterval(function(){
    $.ajax({
        type: "POST",
        url: "online.php",
        complete: function(jqXHR, textStatus){
            $("#online").html(jqXHR.responseText);
        }
    });
},300000);

PHP

<?php 
session_start();
include('controller/class/RO_dbconfig.php');

$sth = $dbconnect->prepare("SELECT first_name,keyId FROM register WHERE status = :online");
$params = array("online" => 1);
$sth->execute($params);

$status = $sth->fetchAll();

echo "<ul  class='online'>";
foreach($status as $onlineArray){
    echo "<li><a href='timeline.php?profileId=" . $onlineArray['keyId'] . "'>" . $onlineArray['first_name'] . "</a></li>";
}
echo "</ul>";
?>

this would call for the list every 5 minutes, and update the div#online. I still don't think this is a needed step. I would just call the ajax on pageLoad, or on hashChange if you work with hashed urls.

footnote, this code has not been tested, so it is possible that I wrote some mistakes here and there.

Pjetr
  • 1,372
  • 10
  • 20