您好我有2个选择相同的表格:
<form role="form" method="post" action="controlreservas/agregareserva.php" >
<!--ID RESIDENCIAL-->
<?php
include "controlreservas/conexion.php";
$sql1= "select id_residencial from residenciales";
$query = $con->query($sql1);
?>
<label>Residencial</label>
<select name="id_residencial">
<?php if($query->num_rows>0):?>
<?php while ($r=$query->fetch_array()):?>
<option value="<?php echo $r["id_residencial"]; ?>"><?php echo $r["id_residencial"]; ?></option>
<?php endwhile;?>
<?php else:?>
<?php endif;?>
</select>
</div>
<!--ID RESIDENCIAL-->
<!--ID HABTIACION (NUMERO HABITACION)-->
<?php
include "controlreservas/conexion.php";
$sql1= "select id_residencial,id_habitacion
from habitaciones
where id_residencial=".$_POST["id_residencial"];
$query = $con->query($sql1);
?>
<label>N° Habitación</label>
<select name="id_habitacion">
<?php if($query->num_rows>0):?>
<?php while ($r=$query->fetch_array()):?>
<option value="<?php echo $r["id_habitacion"]; ?>"><?php echo $r["id_habitacion"]; ?></option>
<?php endwhile;?>
<?php else:?>
<?php endif;?>
</select>
</div>
<!--ID HABTIACION (NUMERO HABITACION)-->
<button type="submit" class="btn btn-default" style="float:left">Ingresar Reserva</button>
</form>
首先选择它正常工作,第二个选择我需要在表格中取值:
where id_residencial=".$_POST["id_residencial"];
,但我不知道这是怎么回事(我看到来自更改值的代码,但没有看到数组:/ )
答案 0 :(得分:0)
您需要一个额外的PHP页面才能使用ajax调用。
<script src="jquery.min.js"></script> // please link the path or use cdn
<script type="text/javascript">
$(document).ready(function(){
$('#id_residencial').on('change',function(){
var id_residencial = $(this).val();
if(id_residencial){
$.ajax({
type:'POST',
url:'get_id_habitacion.php',
data:'id_residencial='+id_residencial,
success:function(html){
$('#id_habitacion').html(html);
}
});
}else{
$('#state').html('<option value="">Select first</option>');
}
});
});
</script>
<!--ID RESIDENCIAL-->
<?php
include "controlreservas/conexion.php";
$sql1= "select id_residencial from residenciales";
$query = $con->query($sql1);
?>
<label>Residencial</label>
<select name="id_residencial">
<?php if($query->num_rows>0):?>
<?php while ($r=$query->fetch_array()):?>
<option value="<?php echo $r["id_residencial"]; ?>"><?php echo $r["id_residencial"]; ?></option>
<?php endwhile;?>
<?php else:?>
<?php endif;?>
</select>
</div>
<!--ID RESIDENCIAL-->
<!--ID HABTIACION (NUMERO HABITACION)-->
<select name="id_habitacion">
</select>
</div>
<!--ID HABTIACION (NUMERO HABITACION)-->
<button type="submit" class="btn btn-default" style="float:left">Ingresar Reserva</button>
</form>
创建一个新的php文件,说&#34; get_id_habitacion.php&#34;代码
<?php
$id_residencial=$_POST["id_residencial"];
$sql2= "select id_residencial,id_habitacion
from habitaciones
where id_residencial='$id_residencial';
$query2 = $con->query($sql2);
?>
<label>N° Habitación</label>
<?php if($query2->num_rows>0):?>
<?php while ($r2=$query2->fetch_array()):?>
<option value="<?php echo $r2["id_habitacion"]; ?>"><?php echo $r2["id_habitacion"]; ?></option>
<?php endwhile;?>
<?php else:?>
<?php endif;?>
注意:1)检查数据库(必须正确连接并有一些数据) 2)检查变量(因为我没有测试并运行代码) 3)如果存在问题,请检查&#34; get_id_habitacion.php&#34;使用&#34; localhost / project_name / get_id_habitacion.php&#34; (记得用联系人变量替换get_id_habitacion.php的第二行,例如$ id_residencial =&#39; 5&#39 ;;) 4)使用jquery.min.js的正确链接或使用其cdn