my initial code in jquery + ajax , tried write in javascript not working now. can tell me where's mistake , why not showing when run through server? checked in console , there no error either
my code in jq
$(document).ready(function(){ findteacher = function() { var file = "course.php?course="+ $("#course").val(); $.ajax({ type : "get", url : file, datatype : "text", success : function(response) { var file2 = response.split(","); $("#courseinfo").html("the course: " + file2[0] + " taught by: " + file2[1]); } }); } clear = function() { $("#courseinfo").html(""); }; $("#course").click(clear); $("#go").click(findteacher); });
my code in js
function findteacher () { var file = "course.php" + document.getelementbyid('course'); function callajax(){ var xmlhttp = new xmlhttprequest(); xmlhttp.onreadystatechange = function(){ if (xmlhttp.readystate == 4 && xmlhttp.status == 200){ document.getelementbyid('courseinfo').innerhtml = xmlhttp.responsetext; } } xmlhttp.open("get", file, true); xmlhttp.send(null); var file2 = callajax.split(","); document.getelementbyid('courseinfo').text("the course: " + file2[0] + " taught by: " + file2[1]); } document.getelementbyid('go').onclick(findteacher) } window.onload = findteacher;
you're missing ?course=
in file. you're not getting .value
of course element. callajax.split(",")
makes no sense -- callajax
function, not string -- should using xmlhttp.responsetext.split(",")
in onreadystatechange function. onclick
property assign to, not method, .onclick(findteacher)
should onclick = findteacher
; , shouldn't inside function, should done once when page loaded.
function findteacher () { var file = "course.php?course=" + document.getelementbyid('course').value; var xmlhttp = new xmlhttprequest(); xmlhttp.onreadystatechange = function(){ if (xmlhttp.readystate == 4 && xmlhttp.status == 200){ var file2 = xmlhttp.responsetext.split(","); document.getelementbyid('courseinfo').innerhtml = "the course: " + file2[0] + " taught by: " + file2[1]; } } xmlhttp.open("get", file, true); xmlhttp.send(null); } function clear () { document.getelementbyid('courseinfo').innerhtml = ''; } window.onload = function() { document.getelementbyid('go').onclick = findteacher; document.getelementbyid('course').onclick = clear; }