The drop down customers is not showing the list of customers.I can not finde what I’am missing. I want to get the list of customers in the first dropdown and than get the list of order numbers of the selected customer. If I manage to get beyond this, then I would like to display the contents of the order in a table. But now, unfortunately, I’m stuck here. Thanky ou if you could help me finde wher I went wrong.
My code:
index.php:
<?php
include 'settings.php';
$sql = "SELECT DISTINCT K_customers.Name, K_customers.Vo FROM K_customers
INNER JOIN K_orders ON K_customers.Vo = K_orders.Vo
WHERE K_customers.Name = '{$_POST['customers']}'";
$result = mysqli_query($conn, $sql);
?>
<div class="container"><br>
<div class="row">
<div class="col-md-3">
<div class="form-group">
<label for="customers">Vevő:</label>
<select id="customers" name="customers">
<option value="">-- Válasszon --</option>
<?php
while($row = $result->fetch_assoc()) {
echo "<option value='".$row['Vo']."'>".$row['Name']."</option>";
}
?>
</select>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="orders">Rendelés:</label>
<select id="orders" name="orders">
<option value="">-- Válasszon --</option>
</select>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function(){
// Vevő kiválasztása eseménykezelője
$('#customers').change(function(){
var customerName = $(this).val();
if(customerName){
// AJAX kérés az adatok lekérdezéséhez
$.ajax({
url: 'get_orders.php',
type: 'post',
data: {customerName: customerName},
dataType: 'json',
success:function(response){
// Töltse fel az options-t a vevőhöz tartozó rendelésekkel
$('#orders').empty();
$('#orders').append('<option value="">-- Válasszon --</option>');
$.each(response,function(index,data){
$('#orders').append('<option value="'+data+'">'+data+'</option>');
});
}
});
}
else{
$('#orders').empty();
$('#orders').append('<option value="">-- Válasszon --</option>');
}
});
});
</script>
get_orders.php:
<?php
include 'settings.php';
$vo = $_POST['customers'];
$sql = "SELECT `order` FROM `K_orders` WHERE `Vo` = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param('s', $vo);
$stmt->execute();
$result = $stmt->get_result();
$orders = array();
while($row = $result->fetch_assoc()) {
$orders[] = $row['order'];
}
echo json_encode($orders);
?>