Я думаю, вы что-то упустили.вот фрагмент
<?php
$connection = mysqli_connect(DB_HOST, DB_USER, DB_PASS,DATABASE); if (mysqli_connect_errno()) echo "Failed to connect to MySQL: " . mysqli_connect_error();
echo '<p>Company <span>*</span></p>
<span class="icon-case"><i class="fa fa-comments-o"></i></span>
<select name="company" id="company">';
echo '<option value="">Please select</option>';
$query="SELECT name,company_id FROM `companies` ORDER BY `name`ASC";
$result=mysqli_query($connection,$query) or die ("Query to get data from sector table failed: ".mysql_error());
while ($row=mysqli_fetch_array($result)) {
$company_id=$row["company_id"];
$company_name=$row["name"];
echo "<option value=".$company_id.">".$company_name." </option>";
}
echo '</select>
</div>
<p>Contact in Company <span>*</span></p>
<span class="icon-case"><i class="fa fa-comments-o"></i></span>
<select name="contact" id="contact">';
<option value=""> Please select</option>
</select>';
?>
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script>
$(document).ready(function(){
$('#company').on('change',function(){
var companyid = $(this).val();
alert('hello');
if(companyid){
$.ajax({
type:'POST',
url:'ajaxData.php',
data:'id='+companyid,
success:function(html){
$('#contact').append(html);
}
});
}
});
});
</script>
Ajaxdata.php
<?php /* Connect to MySQL and select the database. */
$connection = mysqli_connect('localhost', 'root', '','demo'); if (mysqli_connect_errno()) echo "Failed to connect to MySQL: " . mysqli_connect_error();
?>
<?php
$company_id=$_POST['id'];
echo $query="SELECT * FROM `contact` WHERE company_id='".$company_id."'";
$result=mysqli_query($connection,$query) or die ("Query to get data from sector table failed: ".mysqli_connect_error());
while ($row=mysqli_fetch_array($result)) {
$company_id=$row["company_id"];
$contact_id=$row["contact_id"];
echo "<option value=\"$company_id\"> $contact_id</option>";
}