I am using PHP, mysql and ajax. I have one php file called Billing.php
where i have 2 dropdowns one named Company
and the other named Model Name
.
I am able to display all the company names and model names in both dropdown by using sql query.
But now it fetches all the data available in both the dropdown. My concern is that i want to select one company name and second dropdown should filter the model name on the basis of company.
After looking for some solution online i came to know we can use ajax for that but i don't know what i am missing in my code. It is giving me error as well
Notice: Undefined index: ajaxcompanyname in C:\Users\Shesharm\MyWebsite\Billing.php
Where ajaxcompanyname
is the data passed from ajax function to php file which i am using to fetch query result for Model name dropdown.
Billing.PHP
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<style>
table,th,td{
border: 1px solid black;
border-collapse: collapse;
text-align: left;
table-layout: auto;
background-color: #CCCCCC;
}
</style>
<script type="text/javascript" src="Billing_Selection.js"></script>
</head>
<body>
<?php
//Define variables
$company=$ModelName=$ModelNo=$productcolor=$cost=$sql1=$sql2=$IMEI=$accessories=$row=$quantity=$customername=$customerinfo=$prodstatus="";
$quantityerr=$q="";
?>
<form name="SearchMobile" method="POST" action="<?php $_SERVER['PHP_SELF'];?>" style="background-color:#FF6600;">
<fieldset>
<legend><b>Search the Required mobile</b> </legend>
<?php
// Connect to database and get the no of rows in $num
$con = mysqli_connect("MyWebsite.localdomain","root","shekhar123","test_youtube");
$sql1 = "select DISTINCT CompanyName from mobile_data";
//check connection
if(!$con){
echo "Failed to connect to MySQL: " . mysql_error();
}
$retval1 = mysqli_query($con,$sql1);
echo "<select name='companyname' onchange='companysortlist(this.value)'>";
echo "<option value=''>Select a Mobile:</option>";
while($row = mysqli_fetch_array($retval1)){
echo "<option value='".$row['CompanyName']."'>".$row['CompanyName']."</option>";
}
echo "</select>"." ";
//******************* Select the model name..
$sql2 = "select DISTINCT ModelName from mobile_data where CompanyName='".$_POST['ajaxcompanyname']."'";
//check connection
if(!$con){
echo "Failed to connect to MySQL: " . mysql_error();
}
echo "<select name='modelname' class='companymodelname'>";
echo "<option value=''>Select a Model Name:</option>";
$retval1 = mysqli_query($con,$sql2);
while($row = mysqli_fetch_array($retval1)){
echo "<option value='".$row['ModelName']."'>".$row['ModelName']."</option>";
}
echo "</select>"." ";
mysqli_close($con);
?>
</fieldset>
</form>
<br>
</body>
</html>
Billing_Selection.js
function companysortlist(data){
$.ajax({
type:'POST',
url:'Billing.php',
data:"ajaxcompanyname="+data,
success:function(data){
$('.companymodelname').html(data);
}
}
)
}
Please let me know what i am doing wrong.I am beginner in all these.