It works on input fields, but not working in select fields.
So far this code is working only in input field, my select fields are: region,province and city the rest are inputs
<script>
function GetDetail(str) {
if (str.length == 0) {
document.getElementById("firstname").value = "";
document.getElementById("lastname").value = "";
document.getElementById("email").value = "";
document.getElementById("region").value = "";
document.getElementById("province").value = "";
document.getElementById("city").value = "";
document.getElementById("storename").value = "";
return;
}
else {
// Creates a new XMLHttpRequest object
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (this.readyState == 4 &&
this.status == 200) {
var myObj = JSON.parse(this.responseText);
document.getElementById("firstname").value = myObj[0];
document.getElementById("lastname").value = myObj[1];
document.getElementById("email").value = myObj[2];
document.getElementById("region").value = myObj[3];
document.getElementById("province").value = myObj[4];
document.getElementById("city").value = myObj[5];
document.getElementById("storename").value = myObj[6];
}
};
// xhttp.open("GET", "filename", true);
xmlhttp.open("GET", "gfg.php?mobile=" + str, true);
// Sends the request to the server
xmlhttp.send();
}
}
</script>
<?php
include 'server/config.php';
// Get the user id
$user_id = $_REQUEST['mobile'];
if ($user_id !== "") {
$stmt = $database->prepare("SELECT firstname,lastname,email,region,province,city,storename FROM records_db WHERE mobile = :mobile");
$stmt->bindParam(':mobile',$user_id);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_OBJ);
$firstname = $row->firstname;
$lastname = $row->lastname;
$email = $row->email;
$region = $row->region;
$province = $row->province;
$city = $row->city;
$storename = $row->storename;
}
// Store it in a array
$result = array("$firstname", "$lastname", "$email", "$region", "$province", "$city", "$storename");
// Send in JSON encoded form
$myJSON = json_encode($result);
echo $myJSON;
?>
Once I enter a mobile number, the records of that mobile number will automatically display on the input fields also in the select fields, but unfortunately the select fields are not working.