weixin_33738578 2011-08-24 03:59 采纳率: 0%
浏览 59

为什么AJAX代码在PHP中不起作用?

JS 和 HTML:

<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
function load_bkissue(accessid){
   //alert(accessid);   
        $.ajax({
    type: "POST",
    url: "2.php",
    async: false,
    dataType: "json",
    data: "accession_id=" + accessid,
    success: function (response) {
        $("#bkid").val(response.accession_id);
        $("#txtbnam").val(response.title);
        $("#txauth").val(response.author);
        $("#txtcat").val(response.category);
        $("#txtsub").val(response.subject_type);
        //$("#txt_div").val(response.rack_no);
        $("#txtrack").val(response.rack_no);
    }
});
        </script>
</head>
<body>
<table width="260" id="tab" cellpadding="1" cellspacing="10">
        <tr>
            <td width="20px"> AccessionNo </td>
            <td width="400px">
                <select name="bkid" onChange="load_bkissue(this.value)" class="txtbox">
            <option value="1">1 </option>
            <option value="2">2 </option>
            <option value="3">3 </option>
            <option value="4">4 </option>
            <option value="5">5 </option>

                        </select>           </td>
        </tr>
        <tr>
            <td> Title </td>

            <td><input name="txtbnam" id="txtbnam" type="text"  class="txtbox"></td>
        </tr>
        <tr>
            <td> Author </td>
            <td><input name="txauth" id="txauth" type="text" id="txauth" class="txtbox"></td>
        </tr>
        <tr>
            <td> Category </td>

            <td><input name="txtcat" id="txtcat" type="text" id="txtcat"  class="txtbox"></td>
        </tr>
        <tr>
            <td> Subject </td>
            <td><input name="txtsub" id="txtsub" type="text" id="txtsub"  class="txtbox"></td>
        </tr>
        <tr>
            <td> Rack </td>

            <td><input name="txtrack" id="txtrack" type="text" id="txtrack" class="txtbox"></td>
        </tr>
        </table>

</body>
</html>

PHP:

<?php
$json = array(
                'accession_id' => "a",
                'title'        => "b",
                'author'       => "c",
                'category'     => "d",
                'subject_type' => "e",
                'rack_no'      => "f");
echo json_encode($json );
?>

选择AccessionNo时,我需要在每个字段(例如标题,作者等)中使用这些值,但没有任何变化。

  • 写回答

2条回答 默认 最新

  • weixin_33720956 2011-08-24 04:21
    关注

    Your JSON, is...weird. Why not just do it like this:

    <?php
    $json = array(
                    'accession_id' => "a",
                    'title'        => "b",
                    'author'       => "c",
                    'category'     => "d",
                    'subject_type' => "e",
                    'rack_no'      => "f");
    echo json_encode($json);
    ?>
    

    This results in JSON that looks like this (much more sane):

    {
        "accession_id":"a",
        "title":"b",
        "author":"c",
        "category":"d",
        "subject_type":"e",
        "rack_no":"f"
    }
    

    And makes your javascript much simpler:

    $.ajax({
        type: "POST",
        url: "http://temp.lmfast1/testajax/2.php",
        async: false,
        dataType: "json",
        data: "accession_id=" + accessid,
        success: function (response) {
            $("#bkid").val(response.accession_id);
            $("#txtbnam").val(response.title);
            $("#txauth").val(response.author);
            $("#txtcat").val(response.category);
            $("#txtsub").val(response.subject_type);
            //$("#txt_div").val(response.rack_no);
            $("#txtrack").val(response.rack_no);
        }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler