Welcome Guest, Not a member yet? Register   Sign In
Problem updating a div with ajax (json_encode)
#1

[eluser]Unknown[/eluser]
Hello everyone Smile
Im stuck for 2 days now , and i need your help.
I want to use ajax to update a div with the class .user-responce,every time the database column "status" is updated. I have a problem though, manipulating the json object created by the ajax controller and return it back at the view from jquery. This $data variable into controller makes my life hard at the moment and i keep getting errors.
I please for your help and thanks in advanceSmile)


Here is my index controller

Code:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home_page extends CI_Controller {

public function index() {

//$this->load->model('home_page_model');
//$data['query'] = json_encode($this->home_page_model->onlineUsers());
//$this->load->view('homepage',$data);
$this->load->view('homepage');
}
}
?>

This is my Ajax_controller.php
Code:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Ajax_controller extends CI_Controller {
public function __construct() {
        parent::__construct();
    }
public function GetOnlineUsers() {
$this->load->model('home_page_model');
$data['query'] = json_encode($this->home_page_model->onlineUsers());
$this->load->view('homepage',$data);
  }
}
?>

My Model home_page_model.php
Code:
<?php
class home_page_model extends CI_Model {
function __construct() {
  parent::__construct();
}

function onlineUsers() {
  $this -> db -> select('user_name,status,thumb') -> from('users') -> where('status', 'Online');
  $query = $this -> db -> get();
  if ($query -> num_rows > 0) {
  return $query -> result();
} else {
  'There are no results';
  }
}
}
?>

And finally my View

Code:
<!DOCTYPE html>
&lt;html lang="en"&gt;
&lt;head&gt;
  &lt;meta http-equiv="cache-control" content="no-cache"&gt;
  &lt;meta charset="utf-8"&gt;
  &lt;title&gt;Welcome!&lt;/title&gt;
        &lt;link rel="stylesheet" type="text/css" href="&lt;?php echo base_url()?&gt;css/styles.css"&gt;
[removed][removed]

[removed]
setInterval(function()
{
$('.user-responce').load('&lt;?php echo base_url()?&gt;Ajax_controller/GetOnlineUsers');
}, 2000);
[removed]
&lt;/head&gt;
&lt;body&gt;
  <div id="container">
   <h1>Welcome to CodeIgniter!</h1>

<div id="body">
<div class="user-responce">
&lt;?php

print_r($query);


?&gt;
</div>
</div>
<p class="footer">
Page rendered in <strong>{elapsed_time}</strong> seconds
</p>
</div>
&lt;/body&gt;
&lt;/html&gt;
#2

[eluser]LuckyFella73[/eluser]
Your Ajax controller has to return the result as json.
You can't just load a view in your controller to get a json result.

Your controller function should more look like:
Code:
public function GetOnlineUsers()
{
$this->load->model('home_page_model');
die(json_encode($this->home_page_model->onlineUsers())); // return result to ajax function
}

If you want to format (HTML) the result before the data is send to the js function
you can do that too.

EDIT:
I looked at your controller code and saw your json_encode function so
I thought you expect a json as the result for your js function. But
now I see you use the jquery "load" function. So my code might not
help you.

Can you post what kind of errors do you get?




Theme © iAndrew 2016 - Forum software by © MyBB