Welcome Guest, Not a member yet? Register   Sign In
Help with ajax and codeigniter
#1

[eluser]Stefanos Ka[/eluser]
Hi all,
I'm using ajax to update a page by calling a php file..
In the php file where i want to return the data(for updating the page) i want to call the "$this->load->model('etc')" for getting the data from a db and i can't..
It returns "Fatal error: Using $this when not in object context in.."
Any help???
Thanks in advance..
#2

[eluser]naren_nag[/eluser]
It's hard to figure out what you're doing. So, here's how I'd do it:

1. Create a method in a controller, let's call the controller RPC and let's call the method names
Code:
class Rpc extends CI_Controller {

// snip
  
function names() {

   $this->load->model("users");
  
   // load ten names in an array called $data

   echo json_encode($data);
}
}

In my view, I would just post the data using Javascript to <url>/rpc/names and get the data back. It will work fine.

Now, if you share your code, perhaps we can figure out what's wrong.
#3

[eluser]Stefanos Ka[/eluser]
Hi naren_nag, thanks for your reply but it didn't help me..Confused
My code is as follow..

The Javascript part(Ajax):
Code:
function loadXMLDoc()
            {
                var xmlhttp;
                document.getElementById("btnupdate")[removed] = "Updating...";
                if (window.XMLHttpRequest)
                  {// code for IE7+, Firefox, Chrome, Opera, Safari
                  xmlhttp=new XMLHttpRequest();
                  }
                else
                  {// code for IE6, IE5
                  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                  }

                var user_name = document.getElementById("user_name").value;
                xmlhttp.open("GET","system/application/views/get_trans.php?user_name=" + user_name,false);
                xmlhttp.send();
                document.getElementById("div2update")[removed]=xmlhttp.responseText;
            }

And, the "get_trans.php" file:

Code:
&lt;?php
if ($_GET['user_name'])
    {
        $this->load->Model('Mdvd','',true); //Here the error come up..
        $user_name =$_GET['user_name'];
        $query = $this->Mdvd->GetTransByName($user_name);

        echo 'Hello <b>' . $user_name . '</b>' ;
        // And etc..
    }
?&gt;

I tried with the "mysql_connect" and etc functions and works but i want to load my existing functions from my model...
#4

[eluser]naren_nag[/eluser]
I'm afraid you need to go back and read the documentation on the MVC model. You can't load a model in a standard PHP file, only in a Controller.




Theme © iAndrew 2016 - Forum software by © MyBB