dongzhong1891 2017-05-07 19:51
浏览 8

选择标记更改时发送值并显示输出

I new in term of using jQuery.

I practice using native php ajax, but for this time I need to learn jQuery for the current technology and demand.

I sent "types" value method POST to other page (ajaxInfo.php) when the tag change.

After the select tag change, it should show the result at <div id="showList"> that come from database (MySQL). But nothing happen.

Below are the source code.

Body

<select id="form-types" class="col-xs-10 col-sm-5" name="types">
    <option value="">PLEASE CHOSE</option>
    <option value="STATE">STATE</option>
    <option value="FACULTY">FACULTY</option>
    <option value="PROGRAME">PROGRAME</option>
</select>

<div id="showList"></div>

jQuery AJAX

<script type = "text/javascript" >
$(document).ready(function () {
  $("select#form-types").change(function () {
    var types = $("select#form-types").val();
    if (types != null) {
      $.ajax({
          type: 'post',
          url: 'ajaxInfo.php',
          data: "types=" + types,
          dataType: 'html',
          success: function (response) {
            $("#showList").html(response);
          }
        }
      });
  });
}); 
</script>

Post Page (ajaxInfo.php)

<?php
if (isset($_POST["types"]) === TRUE){
    $types = $_POST["types"];
}
else{
    $types = null;
}
include '../dbco.php';
$query = $dbc -> query ("SELECT child FROM infobase WHERE parent='$types'");
if ($query -> num_rows > 0){
    echo "LIST OF : " . $types . "REGISTERED<br />";
    $count = 1;
    while ($result = $query -> fetch_assoc()){
        echo "$count" . $result['child'] . "<br />";
        count++;
    }
}else{
    echo "NO " . $types . " REGISTERED";
}
?>

Thank You.

  • 写回答

3条回答 默认 最新

  • douchilian1009 2017-05-07 20:00
    关注

    You are using id (form-types) for your select input field. but your are tying to targeting another id (form-jenis).

    use same named id for select input field and in your jquery selector.

    <script type="text/javascript">
    $(document).ready(function(){
        $("select#form-types").change(function(e){
            e.preventDefault();
    
            var types= $("select#form-types").val();
    
            if (types!= null)
            {
                $.ajax({
                type: 'post',
                url: 'show.php',
                data: "types=" + types,
                dataType: 'html',
                success: function(response) 
                {
                    $("#showList").html(response);
                }
            }
        });
    });
    

    评论

报告相同问题?

悬赏问题

  • ¥15 制裁名单20240508芯片厂商
  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接