So I have a function on a controller on this path usercontroller(controller name/ajaxp(controller function). That look like this, nothing fancy:
public function ajaxp(){
echo "received";
}
Here is the path where I want my AJAX call to get.
I have a select box that look like this :
<label for="exampleInputEmail1">Apartament</label>
<select onchange="showUser(this.value)" name ="txtApartament1" class="form-control">
<?php foreach($getEntry as $value) { ?>
<option><?php echo $value->apartament ?></option>
<?php }?>
</select>
onchange will trigger this AJAX :
<script>
function showUser(str) {
var url = <?php base_url();?>+"usercontroller/ajaxp?q="+str;
console.log(url);
if (str == "") {
document.getElementById("txtApartament1").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("txtApartament1").innerHTML = this.responseText;
}
};
xmlhttp.open("GET",url ,true);
xmlhttp.send();
}
}
</script>
But my url will be :
(index):258 GET http://localhost/adminigniter1/NaN65 404 (Not Found)
insted of:
http://localhost/adminigniter1/usercontroller/ajaxp
And the console.log output will be:
NaN65
65 is the actual str from my select box that I want to sent to the cotroller. And the NaN part I think comes from this part :
var url = <?php base_url();?>+"usercontroller/ajaxp?q="+str;
Where I try to append the controller function path, that string is seen by javascript by NaN and doesn't return the corect URL. There is a way to fix it? (I'm quite new with javascript).
</div>