mark mark - 1 month ago 6
Ajax Question

how to call php file using ajax in codeigniter MVC view file

I am using codeigniter's MVC and I am trying to use Ajax to load a function from my controller file.

My Ajax code loads the php file perfectly without the codeigniter MVC. But in codeigniter I am unsure how to call the php file/function inside the Ajax function.

My code looks like this without codeigniter and it works fine

<script>
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
////////////////////////////////////////////
//////// This loads find without MVC////////
////////////////////////////////////////////
xmlhttp.open("GET","ajax-php.php?q="+str,true);
xmlhttp.send();
////////////////////////////////////////////
//////// This loads find without MVC////////
////////////////////////////////////////////
}
}
</script>


But it doesnt work in codeigniter's view file as shown below

<script>
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
////////////////////////////////////////////
//////// this is problem////////////////////
////////////////////////////////////////////
xmlhttp.open("GET","<?= base_url(); ?>" + "/controller_page/ajax_file.php?q="+str,true);
xmlhttp.send();
////////////////////////////////////////////
//////// this is problem////////////////////
////////////////////////////////////////////
}
}
</script>


So the problem is in the code below,( which has been cut out from the above code to make it easier to read) as I am not sure how to load the function inside the script in an MVC envirosment for codeigniter

////////////////////////////////////////////
//////// this is problem////////////////////
////////////////////////////////////////////
xmlhttp.open("GET","<?= base_url(); ?>" + "/controller_page/ajax_file.php?q="+str,true);
xmlhttp.send();
////////////////////////////////////////////
//////// this is problem////////////////////
////////////////////////////////////////////


Thanks in advance

Answer Source

Your path should be:

xmlhttp.open("GET","<?= base_url(); ?>" + "/controller_name/controller_function_name?q="+str,true);
xmlhttp.send();

Lets assume controller_name= user and controller_function_name = my_test_ajax. Then controller user should look like as follows:

class User extends CI_Controller {
     public function __construct(){
        parent::__construct();
        // load model helper etc 
     }

     public function my_test_ajax(){
        // get q
        $qry= $_GET['q'];
        echo $qry;
     }
}

Hope it should help you.