By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,968 Members | 1,843 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,968 IT Pros & Developers. It's quick & easy.

Can not receive server response by using ajax

P: 1
I want to use ajax to fill input fields depending on the select element

Expand|Select|Wrap|Line Numbers
  1. <select name="service" onchange="fillFields(this.options[this.selectedIndex].value)">
Js code looks like:

Expand|Select|Wrap|Line Numbers
  1. function fillFields(inc){
  2. var xmlhttp = new XMLHttpRequest();
  3. xmlhttp.onreadystatechange = function() {
  4.     if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
  5.         var data = JSON.parse(xmlhttp.responseText);
  6.         document.getElementById("datepicker").innerHTML = data.datebeg;
  7.         document.getElementById("ph").innerHTML = data.phone;
  8.         var form = document.forms.mainForm;
  9.         var towns = form.elements["towns[]"];
  10.         var hours = form.elements["hours[]"];
  11.         var minutes = form.elements["minutes[]"]
  12.         var i;
  13.         for (i = 0; i < data.towns.length; i++){
  14.             towns[i] = data.towns[i];
  15.             hours[i] = data.hours[i];
  16.             minutes[i] = data.minutes[i];
  17.         }
  18.     }
  19. };
  20. xmlhttp.open("GET", "fillForm.php?inc=" + inc, true);
  21. xmlhttp.send();
  22. }
fillForm.php retrieves data from db and sends response in JSON format.

It seems to me, I do everything exactly like in examples, but (xmlhttp.readyState == 4) condition fails. ReadyState equals to one. What is my problem?
Mar 21 '16 #1
Share this Question
Share on Google+
1 Reply


Dormilich
Expert Mod 5K+
P: 8,639
if you only ever get a readyState of 1 (and not cancelling the process) then either line 21 produces an error or your server doesn’t respond at all.
Mar 21 '16 #2

Post your reply

Sign in to post your reply or Sign up for a free account.