doukan4039 2015-03-23 16:08
浏览 77
已采纳

JSON无法使用OnChange事件

I originally coded this piece to bring back a single data point. I realized I needed more fields returned with an onchange event (drop down select) and changed it to use JSON. It is not returning any data. The drop down is built dynamically through PHP when the page is first loaded. I'm new to this, so any help would be much appreciated.

Dropdown Code:

<p id="dropdown"   style="DISPLAY: none" >
        <?php
            $query = "call test.spsMSTR_AllCatListBuild";
            $stmt = $conn->query( $query );

            $dropdown = "<select id='catlist' name='catlist' onchange='getval(this);'>";
                $dropdown .= "
<option value= 'NA'>Select Category</option>";
                foreach ($stmt as $row) {
                  $dropdown .= "
<option value='{$row['ID']}'>{$row['RPT_NAME']}</option>";
                }

                $dropdown .= "
</select>";
                echo $dropdown;
                $conn = null;
        ?>
    </p>

Ajax/JSON code:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
<script id="source" language="javascript" type="text/javascript">

       $(document).ready(function(){
            $("#catlist").change(function(){
            var vid = document.getElementById("catlist").value;

            $.getjson("ajax.php",
            {catid:vid},
            function(result){
                alert(result); }
                .error(function(xhr) {
                    alert(xhr)
                })
                ; )
        })})

  </script>

PHP Code:

<?php include('./DBConfig.php'); ?>
<?php

    $vid = $_GET["catid"];
    $query =  "SELECT RPT_NAME, ACTIVE FROM test.MSTR_REPORT_MASTER WHERE ID = $vid" ;
    $stmt = $conn->query( $query );
    //$result = $stmt->fetchColumn();
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    echo json_encode($result);
?>
  • 写回答

2条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 模型导入SP后贴图纹理只有一个,拆了四张UV的,怎么解决?
      • ¥15 检索带order by 非常慢
      • ¥20 python 爬虫 token 加密方式
      • ¥20 IAR环境报错reading CPU status failed
      • ¥15 数组问题,刚学C语言
      • ¥20 Python如何读取指定播放设备的音频帧?
      • ¥15 用dnspy修改游戏数据报错
      • ¥15 关于unity打包成webgl后页面上不显示服务端接收到的数据的问题
      • ¥16 (大一)有偿 想学会 excel数据处理和分析🥹
      • ¥15 matlab导入工具箱提示:此类型的变量不支持使用点进行索引错误