I have this code :
<form id="frmKat" role="form" action="" method="post">
Country :
<select name="country" id="country">
<option>-select your country-</option>
<?php
$result=mysqli_query($kon, "SELECT * from regios");
while($country=mysqli_fetch_assoc($result)){
echo "<option value=$country[id]>$country[naam]</option>";
} ?>
</select>
City :
<select name="city" id="city">
<option>-select your city-</option>
</select>
<div style="clear:both;"></div>
<button type="submit" class="pull-right btn btn-success" name="btnConvert" id="btnConvert">Convert</button>
</form>
My ajax code is :
<script type="text/javascript">
$(document).ready(function(){
$("#country").change(function(){
var country=$("#country").val();
$.ajax({
type:"post",
url:"getcutoff.php",
data:"country="+country,
success:function(data){
$("#city").html(data);
}
});
});
});
</script>
And my getcutoff.php file is next :
<?php
session_start();
include("config.php");
global $kon;
ob_start();
if(isset($_SESSION["admin"])){
$country=$_POST["country"];
$result=mysqli_query($kon, "select * FROM cutoffs WHERE regio_id=". $country ." ");
while($city=mysqli_fetch_array($result)){
echo "<option value=$city[id]>$city[datetime]</option>";
}
}else{
ob_flush();
die("Salut");
}
?>
I'm trying to make dependable selectboxes. When user chooses something in the first selected box, then they will be taken to the second one where they can choose values which are connected to the first selected box. In first box I get values, but when I choose something in the second one, I get nothing. Where am I making a mistake?
Group your options in a php variable as string and use a single echo in getcutoff.php
$res ='';
while($city=mysqli_fetch_array($result)){ $res.="<option value=$city[id]>$city[datetime]</option>"; }
echo $res;
your echo looks a bit wrong. Also, I assume you have saved your daetime as DateTime in SQL, thus you'd need to convert it before defining it as a string.
$date = strtotime($city[datetime]);
$date = date('Y-m-d H:i:s', $date);
echo "<option value='".$city[id].">".$date."'>My Option</option>";