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