我试图通过ajax post将select的值发送到另一个select,实际上我得到了idf中的值而不是数据中的值,console.log(data)
我得到了页面的所有html
<script>
$(document).ready(function () {
$('#specialite_id').change(function(){
var idf=$(this).val();
//var $id=$(this).serialize();
event.preventDefault();
$.ajax({
url : "index.php",
type : "POST",
data:'idf='+idf,
datatype: "text",
success : function(data){
$('#section').html(data);
console.log(data);
},
error : function(){
}
});
});
})
</script>
这是html:
<select id="specialite_id">
<?php foreach( $rowAll as $row )
{
?>
<option value=<?php echo $row['id_specialite']; ?> > <?php echo $row['specialite']; ?> </option>
<?php
}
?>
</select>
<select id="section">
<?php
if(isset($_POST['idf'])){
//$idf = $database->escape_string($_POST['idf']);
$idf=$_POST['idf'];
if(!empty($idf)){
$sql3 = "SELECT section FROM section where id_specialite=:idf ";
$stmt3 = $connexion->prepare($sql3);
$stmt3->BindParam(':idf',$idf);
$stmt3->execute( );
$rowAll3 = $stmt3->fetchAll(PDO::FETCH_BOTH);
foreach( $rowAll3 as $row3)
{
?>
<option > <?php echo $row3['section']; ?> </option>
<?php } ?>
</select>
为什么它不起作用......任何人都有想法?
答案 0 :(得分:0)
我无法将值发送到同一页面,因此我创建了一个新页面“fetch.php”:
$sql2 = "SELECT * from section where id_specialite =:idf ";
$stmt2 = $connexion->prepare($sql2);
$stmt2->BindParam(':idf',$_POST["idf"]);
$stmt2->execute( );
$rowAll2= $stmt2->fetchAll(PDO::FETCH_BOTH);
$output='<option value="">'.$_POST["idf"].'</option>';
foreach( $rowAll2 as $row2)
{
$output ='<option value="'.$row2["id_section"].'">'.$row2["section"].'</option>';
echo $output;
}
我更改了脚本的代码:
<select id="section" name="section">
<option value=" "> selectioner section </option>
</select>
</form>
<script>
$(document).ready(function () {
$('#specialite_id').change(function(){
var idf=$(this).val();
console.log(idf);
$.ajax({
url : "fetch.php",
type : "POST",
data:{idf:idf},
datatype:"text",
success:function(data){
$('#section').html(data);
console.log(data);
}
});
});
})
</script>
它有效!!
以及更多信息: https://youtu.be/T0Q0kEyEK_k
答案 1 :(得分:-1)
我认为你正在以错误的形式传递json,请尝试以下方法:
data:"{'idf':"+idf+"}",