I have made form in which I want to give the ID of an order in a specific field and AJAX will fetch data from database and autofill the remaining fields as per database info.
Here is my FORM –
<form name="confirm_order_entry_form" method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>">
<div class="form_data">
<label>Projection Code :</label>
<input type="text" name="projection_code" onchange="projection_code_data_fetch(this.value)">
</div>
<div class="form_data">
<label>Marketing By :</label>
<input type="text" name="c_marketing_person" id="marketingPerson">
</div>
<div class="form_data">
<label>Factory Name :</label>
<input type="text" name="c_factory_name" id="factoryName">
</div>
<input type="submit" value="submit" name="submit">
</form>
Here is my javascript function –
function projection_code_data_fetch(input_data){
if (input_data==""){
return;
}
if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}else{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
var data = JSON.parse(xmlhttp.responseText);
for(var i=0;i<data.length;i++){
document.getElementById("marketingPerson").value = data[i].name;
document.getElementById("factoryName").value = data[i].factory;
}
}
}
xmlhttp.open("GET","formUp.php?q="+input_data,true);
xmlhttp.send();
}
And here is formUp file –
<?php
$servername='localhost';
$username='root';
$password='';
$dbname = "xcel-erp";
$conn=mysqli_connect($servername,$username,$password,"$dbname");
if(!$conn){
die('Could not Connect MySql Server:' .mysql_error());
}
$q = intval($_GET['q']);
$sql="SELECT * FROM projectiondatabase WHERE System_ID = '".$q."'";
$result = mysqli_query($conn,$sql);
$info = array();
while($row = mysqli_fetch_array($result)){
$cMarketingPerson = $row['MarketingPerson'];
$cFactoryName = $row['FactoryName'];
$info[] = array( 'name' => $cMarketingPerson, 'factory' => $cFactoryName);
}
echo json_encode($info);
?>
I have run the javascipt & formUp file individually. Both are running fine. But the actual output is not coming when combined.
Please help!