I created a php page to process data. Contains HTML form and JavaScript function. Tested the page and it runs well and perfectly. Eg pageprocessor.php
On my index page , each time the service is trigged, ajax fetches the required page (pageprocessor) into a section of index page that will process the data. However, upon retrieval,everything runs well except select inputs(LGA and Model) whose value change on event change (onchange functions) . indicating that my JavaScript function is not running on index page upon ajax call. Only on processor page. How do I solve this please
var ajax;
if(window.XMLHttpRequest) {
ajax=new XMLHttpRequest();
}else{
ajax=new ActiveXObject('Microsoft.XMLHTTP');
} ajax.onreadystatechange=function(){
if(this.readyState ==4 && this.status == 200){
document.getElementById('airtimediv').innerHTML=this.responseText;
document.getElementById('airtimediv').style.visibility="visible";
}
}
ajax.open('GET','insurance.php',true);
ajax.send();
Processor page data
<?php
if(isset($_GET['brand'])){
$vbrand=$_GET['brand'];
echo "
<option value='' disabled selected>...Please Choose ...</option>
";
// get vehicle brand model codes
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://base url/$vbrand",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_POSTREDIR => 3,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"api-key:key here",
"public-key:key here",
"Accept: application/json",
"Content-Type: application/json"
),
));
$response= curl_exec($curl);
curl_close($curl);
$iresponse=json_decode($response);
$code=array_column($iresponse->content->original,'VehicleModelCode');
$name=array_column($iresponse->content->original,'VehicleModelName');
$model=array_combine($code,$name);
foreach ($model as $vmc => $vmn){
echo "
<option value='$vmc'>$vmn</option>
";
}
exit();
}
elseif (isset($_GET['state'])){
$state=$_GET['state'];
echo "
<option value='' disabled selected>...Please Choose ...</option>
";
// get lga code
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://base URL /lga/$state",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_POSTREDIR => 3,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"api-key:",
"public-key:PK_",
"Accept: application/json",
"Content-Type: application/json"
),
));
$response= curl_exec($curl);
curl_close($curl);
$iresponse=json_decode($response);
$code=array_column($iresponse->content->original,'LGACode');
$name=array_column($iresponse->content->original,'LGAName');
$lga=array_combine($code,$name);
foreach ($lga as $lc => $ln){
echo "
<option value='$lc'>$ln</option>
";
}
exit();
}
echo "Maintenance in progress
</br>
<label for='owner'>Owner Name</label>
<input type='text' name='owner' id='owner' class='form-control' required>
</br>
<label for='chasis'>Chasis Number</label>
<input type='text' name='chasis' id='chasis' class='form-control' required>
</br>
<label for='email'>Owner Email</label>
<input type='text' name='email' id='email' class='form-control' required>
</br>
<label for='phone'>Owner Phone</label>
<input type='tel' name='phone' id='phone' class='form-control' required>
</br>
<label for='Year'>Year of Make</label>
<input type='tel' name='year' id='year' class='form-control' required>
</br>
<label for='color'>Vehicle Color</label>
</br> <select name='color' id='color' class='form-control' required>
<option value='' disabled selected>...Please Choose...</option>
";
// get color codes
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://base URL /color",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_POSTREDIR => 3,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"api-key:",
"public-key:PK_",
"Accept: application/json",
"Content-Type: application/json"
),
));
$response= curl_exec($curl);
curl_close($curl);
$iresponse=json_decode($response);
$code=array_column($iresponse->content,'ColourCode');
$name=array_column($iresponse->content,'ColourName');
$colour=array_combine($code,$name);
foreach ($colour as $vcc => $vcn){
echo "
<option value='$vcc'>$vcn</option>
";
}
echo "</select>
</br>
<label for='engine'>Engine Capacity</label>
</br> <select name='engine' id='engine' class='form-control' required>
<option value='' disabled selected>...Please Choose...</option>
";
// get Engine capacity codes
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://base url/engine-capacity",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_POSTREDIR => 3,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"api-key:",
"public-key:PK_",
"Accept: application/json",
"Content-Type: application/json"
),
));
$response= curl_exec($curl);
curl_close($curl);
$iresponse=json_decode($response);
$code=array_column($iresponse->content,'CapacityCode');
$name=array_column($iresponse->content,'CapacityName');
$capacity=array_combine($code,$name);
foreach ($capacity as $vec => $ven){
echo "
<option value='$vec'>$ven</option>
";
}
echo "</select>
</br>
<label for='brand'>Vehicle Brand</label>
</br> <select name='brand' id='brand' class='form-control' required>
<option value='' disabled selected>...Please Choose...</option>
";
// get vehicle brand codes
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://base url/brand",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_POSTREDIR => 3,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"api-key:",
"public-key:PK",
"Accept: application/json",
"Content-Type: application/json"
),
));
$response= curl_exec($curl);
curl_close($curl);
$iresponse=json_decode($response);
$code=array_column($iresponse->content,'VehicleMakeCode');
$name=array_column($iresponse->content,'VehicleMakeName');
$vehicle=array_combine($code,$name);
foreach ($vehicle as $vbc => $vbn){
echo "
<option value='$vbc'>$vbn</option>
";
}
echo "</select>
</br>
<label for='model'>Vehicle Model</label>
</br> <select name='model' id='model' class='form-control' required>
</select>
</br>
<label for='State'>State</label>
</br> <select name='state' id='state' class='form-control' required>
<option value='' disabled selected>...Please Choose...</option>
";
// get state codes
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://base url/state",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_POSTREDIR => 3,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"api-key:",
"public-key:PK_",
"Accept: application/json",
"Content-Type: application/json"
),
));
$response= curl_exec($curl);
curl_close($curl);
$iresponse=json_decode($response);
$code=array_column($iresponse->content,'StateCode');
$name=array_column($iresponse->content,'StateName');
$state=array_combine($code,$name);
foreach ($state as $vsc => $vsn){
echo "
<option value='$vsc'>$vsn</option>
";
}
echo "</select>
</br>
<label for='lga'>Local Government Area</label>
</br> <select name='lga' id='lga' class='form-control' required>
</select>
";
echo "
<script>
document.getElementById('brand').setAttribute('onchange','model()');
document.getElementById('state').setAttribute('onchange','lga()');
function model(){
var car=document.getElementById('brand').value;
var ajax;
if(window.XMLHttpRequest) {
ajax=new XMLHttpRequest();
}else{
ajax=new ActiveXObject('Microsoft.XMLHTTP');
} ajax.onreadystatechange=function(){
if(this.readyState ==4 && this.status == 200){
document.getElementById('model').innerHTML=this.responseText;
}
}
ajax.open('GET','insurance.php?brand='+car,true);
ajax.send();
}
function lga(){
var state=document.getElementById('state').value;
var ajax;
if(window.XMLHttpRequest) {
ajax=new XMLHttpRequest();
}else{
ajax=new ActiveXObject('Microsoft.XMLHTTP');
} ajax.onreadystatechange=function(){
if(this.readyState ==4 && this.status == 200){
document.getElementById('lga').innerHTML=this.responseText;
}
}
ajax.open('GET','insurance.php?state='+state,true);
ajax.send();
}
</script>
";
?>
Tried creating it o index page so the functions can run, but same result.