I am having problem fetching table through ajax. Here is my ajax function:
function search() {
var JobFunction_Id=document.getElementById("JobFunction_Id").value;
var JobFamily_Id=document.getElementById("JobFamily_Id").value;
var flag=0;
var newreq=createRequest();
if(JobFunction_Id!='' && JobFamily_Id!='') { flag=1; }
if(flag==1) {
var url=("Talent_S_New.php?JobFunction_Id="+JobFunction_Id+"& JobFamily_Id="+JobFamily_Id);
newreq.onreadystatechange=function() {
alert('hi');
if(newreq.readyState==4 && newreq.status==200) {
if(newreq.responseText!='') {
document.getElementById("display_result").innerHTML =newreq.responseText; }
//else {
// document.getElementById("display_result").innerHTML =newreq.responseText;
//}
}
}
newreq.open("GET",url,true);
newreq.send();
}
}
I am trying to fetch output variables with table info from this page.
echo('<table style="border:1px solid red" >');
echo('<th>');
echo('First Name');
echo('</th>');
echo('<th>');
echo('Last Name');
echo('</th>');
echo('<tr>');
echo('<td style="border:1px solid red">');
foreach($Allemp as $key=>$value) {
if (array_key_exists('Error', $Allemp))
{ echo($value); }
else
{ echo($value["Emp_FirstName"]); }
//echo('</td>');
//echo('<td style="border:1px solid red">');
if (array_key_exists('Error', $Allemp))
{ echo($value); }
else
{ echo($value["Emp_LastName"]); }
}
echo('</td>');
echo('</tr>');
echo('</table>');
Without ajax above code works fine. You can observalert('hi'). If I put that alert it goes into if(newreq.readyState==4 && newreq.status==200) loop and result will be shown for a second and disappers again. I think it's going into foreach loop bcos it shows alert 5 times and displays result for a second and disappears. Any idea to fix this?
newreq.readyStateandnewreq.statusinstead of alerting 'hi'?alert('hi')if you put it inside the if-statement? Instead of thedocument.getElementById('display_result')...part, tryconsole.log('response text = '+ newreq.responseText)and see what shows up in your browser console (usually hit F12 to see it).