CodeIgniter Forums
url in an ajax application - Printable Version

+- CodeIgniter Forums (
+-- Forum: Using CodeIgniter (
+--- Forum: General Help (
+--- Thread: url in an ajax application (/showthread.php?tid=66062)

url in an ajax application - rooye - 08-30-2016

Hello friends,

I am designing an application in which I want to pull data from a php file using javascript technique, namely ajax. In CI am not sure how to access my controller this way.

Consider this as my view:

<title>An Ajax example</title>
<script language = "javascript">
var XMLHttpRequestObject = false;
if (window.XMLHttpRequest) {
XMLHttpRequestObject = new XMLHttpRequest();
} else if (window.ActiveXObject) {
XMLHttpRequestObject = new
function getData(dataSource, divID)
if(XMLHttpRequestObject) {
var obj = document.getElementById(divID);"GET", dataSource);
XMLHttpRequestObject.onreadystatechange = function()
if (XMLHttpRequestObject.readyState == 4 &&
XMLHttpRequestObject.status == 200) {
obj.innerHTML = XMLHttpRequestObject.responseText;
<H1>An Ajax example</H1>
<input type = "button" value = "Fetch the message"
onclick = "getData('data.php', 'targetDiv')">
<div id="targetDiv">
<p>The fetched message will appear here.</p>

Thanks for you suggestions

RE: url in an ajax application - InsiteFX - 08-30-2016

PHP Code:
baseUrl "<?php echo base_url(); ?>";

PHP Code:
// Save
$("#save").click(function () {


dataString = {
data: $("#nestable-output").val(),

type    "POST",
url     baseUrl "menu/saveMnu",
data    dataString,
cache   false,
success : function (data) {
alert('Data has been saved');
error: function (xhrstatuserror) {
baseUrl + "controller/method";

RE: url in an ajax application - PaulD - 08-30-2016

That example looks like it is from ajax for dummies.

A raw ajax call is actually quite complicated, which is why so many frameworks and libraries have popped up to make the whole job easier.

For instance in jquery, you would do something like this:
PHP Code:
$.get('/mycontroller/mymethod', function(theResponse){

This will get information from mysite/mycontroller/mymethod just like a normal url request. It will get the response and put it into whatever has the id of 'results'.

And your controller would just do this:
PHP Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');

Mycontroller extends CI_Controller {

 public function 
'Hello World';

However, in the example of jquery, there is much more you can do with the library:

Now you could still load a view as a string (use the third option in the load view and set it to true) for keeping html in views, and you can send data in the url segments or use a post instead of a get. Then you can post an array to the controller, validate it in the usual way, do some database calls, use a view to create a html snippet and return that. The level of complexity you add is up to you.

As for your example you posted, you need to give it the url you are sending the request to. You controller just acts in the normal way apart from echoing the results rather than sending them as full page output.

I hope that helps,


RE: url in an ajax application - InsiteFX - 08-31-2016

A javascript file can not execute php code only a file with the php extension can execute php code.

RE: url in an ajax application - PaulD - 08-31-2016


No of course it can't. Not sure where you thought I was saying that but if what I said is not clear then I apologize for any confusion.

RE: url in an ajax application - InsiteFX - 08-31-2016

No not at you PaulD, Just stating it so anyone else will not try to do it.