dongqiuwei8667 2013-06-11 17:38
浏览 43
已采纳

链式ajax选择框

I have some problems with my html/php/ajax code about dependent (or chained) select. I want to show in my menu the list of faculties after I have decided the university. I'll show you my (italian) code. I hope you'll help me. Thanks.

javascript ajax code:

<script type="text/javascript">

$(document).ready(function()
{

$(".universita").change(function()
{
var dataString = 'id='+ $(this).val();
$.ajax
({
type: "POST",
url: "ajax_facolta.php",
data: dataString,
cache: false,
success: function(html)
{
$(".facolta").html(html);
} 
});

}); 

});

html code about two select boxes:

<td align="right">Università:   </td>
    <td>
    <select class="input" name="universita">
        <option selected="selected">--Seleziona Università--</option>
    <?php 

        require('config.php');

        $query = mysqli_query($con, "SELECT * FROM UNIVERSITA order by id ASC");

        $num_righe = mysqli_num_rows($query);

        for($x=0; $x<$num_righe; $x++)
            {
            $rs = mysqli_fetch_row($query);
                            $id = $rs[0];
            $nome = $rs[1];
    ?>
            <option value="<?php echo $id;?>"> <?php echo $nome; ?></option>
    <?php
            }
    ?>
    </select></td>
</tr>
<tr>
    <td align="right">Facoltà: </td>
    <td><select class="input" name="facolta">
            <option selected="selected">--Seleziona Facoltà--</option>
        </select></td>
</tr>

the file ajax_facolta.php:

<?php

require('config.php');

if($_POST['id'])
{
    $id=$_POST['id'];
    $sql = mysqli_query($con, "SELECT * FROM FACOLTA WHERE id_univ='$id' ");

    echo '<option selected="selected">--Selziona Facoltà--</option>';

    while($row=mysqli_fetch_array($sql))
        {

        $id=$row['id'];
        $nome=$row['nome'];
        echo '<option value="'.$id.'">'.$nome.'</option>';
        }

}

?>

and the simple configure.php:

<?php

$con = mysqli_connect("127.6.143.130","xxxxx","xxxxx", "jeme");

if (!$con)
  {
  die('Errore nella connessione: ' . mysqli_connect_error());
  }

?>

The database is very simple. UNIVERSITA has (id, nome) FACOLTA has (id, nome, id_univ). I do not find any errors but it does not work. Thanks for the help.

  • 写回答

1条回答 默认 最新

  • dongxing1960 2013-06-11 19:17
    关注

    In HTML you have :

     <select class="input" name="universita">
    

    and you are using

    $(".universita").change(function()
    

    $(".universita") will try to search HTML control having class name "universita"

    So change few things in code. I hope it will work

    1). <select class="input" name="universita" id="universita">
    2). $("#universita").change(function()
    3). <td><select class="input" name="facolta" id="facolta">
    4). $("#facolta").html(html);
    

    let me know is this helpfully?.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度