简体   繁体   中英

php not returning any result to javascript ajax object

I'm trying to use ajax, I've used it before, copied the code from my previous html file and altered the values sent, php file to process the code and I'm using the POST method. However, the php script does not return anything to the html page. I've checked all i can, so I'm here now. function look () { var x = document.getElementsByTagName('option')[document.getElementById("region").selectedIndex].value; 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 and will update
 // modal section in the same page.
 ajaxRequest.onreadystatechange = function(){
   if(ajaxRequest.readyState == 4){
      var ajaxDisplay = document.getElementById("accordion");
  ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
   // Now get the value from user and pass it to
 // server script.
 ajaxRequest.open("POST", "search.php", true);
 ajaxRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
 alert(x);
 ajaxRequest.send("region=" + x);

}

The php code is a simple

<?php
echo "The world is blue.";
?>

yet, it returns nothing to the chosen div.

You've probably not posted enough information for us to answer your question. But, I acknowledge that you probably are not sure what questions you need to ask in the first place.

First, I must agree with @cgf. jQuery or the alike will make this easier.

But lets focus on the problem at hand. We need to know what is happening behind the scenes.

The best way to do this is through a developer tool bar such as firebug. Chrome has one built in.

So, load up chrome and hit F12 . This should bring up a very busy looking pane on the bottom of your window. Select the Network tab and then request your page / trigger your javascript. What output do you get in the network tab? Mainly, under status do you get 200, 500 (server error) or perhaps 404 (not found)? Update your question above with what you see / get.

My hope is that this should point you in the right direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM