Damian Doman
Damian Doman

Reputation: 532

PHP/MySQL/AJAX - Refresh query values with AJAX

I want my header to be consequently refreshed with fresh values from my database.

To achieve it i have created an AJAX post method:

AJAX (edited):

$(document).ready( function () {
  function update() {
  $.ajax({
    type: "POST",
    url: "indextopgame.php",
    data: { id: "<?=$_SESSION['user']['id']?>"},
    success: function(data) { 
        $(".full-wrapper").html(data);
    }
});
      }

  setInterval( update, 5000 );
});

It should pass $_SESSION['user']['id'] to indextopgame.php every 10 seconds.

indextopgame.php looks like that:

PHP PART (edited):

<?php

session_start();

$con = new mysqli("localhost","d0man94_eworld","own3d123","d0man94_eworld");

function sql_safe($s)
{
    if (get_magic_quotes_gpc())
        $s = stripslashes($s);
    global $con;
    return mysqli_real_escape_string($con, $s);
}

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{   
    $id = trim(sql_safe($_POST['id']));

    $data = "SELECT username, email, user_role, fbid, googleid, fname, lname, avatar, energy, energymax, health, healthmax, fame, edollar, etoken, companies, workid, city, function FROM members WHERE id = $id";
    $result = mysqli_query($con, $data);

    if (mysqli_num_rows($result) > 0) {
        while($row = mysqli_fetch_assoc($result)) {

            $_SESSION['user']['user_role'] = $row["id"];
            $_SESSION['user']['fbid'] = $row['fbid'];
            $_SESSION['user']['googleid'] = $row['googleid'];
            $_SESSION['user']['created'] = $row['created'];
            $_SESSION['user']['lastlogin'] = $row['lastlogin'];
            $_SESSION['user']['username'] = $row['username'];
            $_SESSION['user']['fname'] = $row['fname'];
            $_SESSION['user']['lname'] = $row['lname'];
            $_SESSION['user']['email'] = $row['email'];
            $_SESSION['user']['avatar'] = $row['avatar'];
            $_SESSION['user']['energy'] = $row['energy'];
            $_SESSION['user']['energymax'] = $row['energymax'];
            $_SESSION['user']['health'] = $row['health'];
            $_SESSION['user']['healthmax'] = $row['healthmax'];
            $_SESSION['user']['fame'] = $row['fame'];
            $_SESSION['user']['edollar'] = $row['edollar'];
            $_SESSION['user']['etoken'] = $row['etoken'];
            $_SESSION['user']['companies'] = $row['companies'];
            $_SESSION['user']['workid'] = $row['workid'];
            $_SESSION['user']['city'] = $row['city'];
            $_SESSION['user']['function'] = $row['function'];
        }

            echo $_SESSION['user']['energy'];
    }
}
?>

Still this wouldn't update the header with values i want, instead it just makes the header disappear. What's wrong with this code? Maybe there are other, more effective methods to refresh values from MySQL?

EDIT:

I've edited the AJAX / PHP code samples - it's working like that! But how may I echo all those variables? Echoing one after another seems to cause error again, since values will disappear from my header.

EDIT2:

Solved, I made a silly mistake with syntax... Thanks everyone for contributing!

Upvotes: 0

Views: 72

Answers (2)

Ahmed Ginani
Ahmed Ginani

Reputation: 6650

data options is missing in success method

success: function(data) {                      
   $(".full-wrapper").html(data); 
}

Also you should have to echo that content in php file which you want to show in header.

Upvotes: 1

jeroen
jeroen

Reputation: 91734

You are not using the data that is sent back from the server in your ajax call:

success: function() { 
        $(".full-wrapper").html(data); 
    }
});

Should be:

success: function(data) { 
                  ^^^^ the returned data
        $(".full-wrapper").html(data); 
    }
});

You should also check that your php script actually echoes out something useful.

Upvotes: 1

Related Questions