Devendra chauhan Devendra chauhan - 6 months ago 21
Ajax Question

Ajax is not working properly in codeigniter

function get_caste()
{

var ajaxRequest; // The variable that makes Ajax possible!
var course_id = document.getElementById('Course_id').value;
var Entry_Type = document.getElementById('Mode_Of_Adm').value;
if(course_id=="")
{

}
else{


ajaxRequest = new XMLHttpRequest();
ajaxRequest.onreadystatechange = function()
{
if(ajaxRequest.readyState == 4)
{
var ajaxDisplay = document.getElementById('Caste_marks');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
ajaxRequest.open("GET", "<?php echo base_url();?>registrar/AddStudentManually/getcastebycourseentry/" +Entry_Type'/' +course_id true);
ajaxRequest.send();
}

}

</script>


the above Js code fetching cast from getcastebycourseentry function but when i Run This code without ajaxRequest The Code are:

ajaxRequest = new XMLHttpRequest();
ajaxRequest.onreadystatechange = function()
{
if(ajaxRequest.readyState == 4)
{
var ajaxDisplay = document.getElementById('Caste_marks');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
ajaxRequest.open("GET", "<?php echo base_url();?>registrar/AddStudentManually/getcastebycourseentry/" +Entry_Type'/' +course_id true);
ajaxRequest.send();


its works fine. but when i put above ajaxRequest on it its not working?!!
get_caste() Function not working?

Answer
try this will work for you.

<html>
<body>

<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
function get_caste(){
    var ajaxRequest;  // The variable that makes Ajax possible!

    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            var ajaxDisplay = document.getElementById('ajaxDiv');
            ajaxDisplay.innerHTML = ajaxRequest.responseText;
        }
    }

    var course_id = document.getElementById('Course_id').value;
    var Entry_Type = document.getElementById('Mode_Of_Adm').value;

    var queryString = "?Course_id=" + Course_id + "&Mode_Of_Adm=" + Mode_Of_Adm;
    ajaxRequest.open("GET", "<?php echo base_url();?>registrar/AddStudentManually/getcastebycourseentry/" + queryString, true);
    ajaxRequest.send(null); 
}

//-->
</script>



<form name='myForm'>
Max Age: <input type='text' id='Course_id' /> <br />
Max WPM: <input type='text' id='Mode_Of_Adm' />
<br />

<input type='button' onclick='get_caste()' value='Query MySQL' />
</form>
<div id='ajaxDiv'>Your result will display here</div>
</body>
</html>