I want to use ajax to fill input fields depending on the select element
Js code looks like:
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.readyS tate == 4) condition fails. ReadyState equals to one. What is my problem?
Code:
<select name="service" onchange="fillFields(this.options[this.selectedIndex].value)">
Code:
function fillFields(inc){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var data = JSON.parse(xmlhttp.responseText);
document.getElementById("datepicker").innerHTML = data.datebeg;
document.getElementById("ph").innerHTML = data.phone;
var form = document.forms.mainForm;
var towns = form.elements["towns[]"];
var hours = form.elements["hours[]"];
var minutes = form.elements["minutes[]"]
var i;
for (i = 0; i < data.towns.length; i++){
towns[i] = data.towns[i];
hours[i] = data.hours[i];
minutes[i] = data.minutes[i];
}
}
};
xmlhttp.open("GET", "fillForm.php?inc=" + inc, true);
xmlhttp.send();
}
It seems to me, I do everything exactly like in examples, but (xmlhttp.readyS tate == 4) condition fails. ReadyState equals to one. What is my problem?
Comment