weixin_33674976 2014-05-08 18:43 采纳率: 0%
浏览 74

为什么我的AJAX代码不起作用?

I believe this should be pretty trivial, but I cannot figure it out at all. I am doing a small test with sending an AJAX request, and echoing a variable extracted from a drop-down menu.

However, it doesn't seem to be executing whatsoever. I was wondering if someone could help me point out what's wrong?

Here are the relevant snippets from index.php:

<html>
    <?php

    //create a drop down of available accounts
                echo 'Available Accounts: ';

                echo '<select name=dropAccounts class=dropAccounts>';
                //if there is at least one account available
                if (count($accsAvailable) > 0) {
                    echo '<option value=0>---Select an account---</option>'; //default option
                    foreach ($accsAvailable as $account) {
                        //populate from API
                        echo '<option value=' . $account->getId() . '>' . $account->getName() . '</option>';
                    }
                } else {
                echo '<option value=0>---No accounts---</option>'; //else if no accounts exist
            }
            echo '</select>';

            //for available webproperties
            echo '<br> Available Webproperties: ';
            echo '<select name=dropProperties class=dropProperties>';
            echo '<option selected=selected>---Select a webproperty---</option>';
            echo '</select>';

    ?>
</html>

The important thing to take away from the snippet is that the first drop-down menu is called 'dropAccounts'.

here is my JS script for handling an onchange event on 'dropAccounts':

<script type="text/javascript">
    $(document).ready(function()
    {
        $(".dropAccounts").change(function()
        {
            var id=$(this).val();
            var dataString = 'id='+ id;

            $.ajax
            ({
                type: "POST",
                url: "propertyID.php",
                data: dataString,
                cache: false,
                success: function(html)
                {
                    $(".dropProperties").html(html);
                } 
            });

        });

    });
</script>

I eventually intend to populate the second drop-down menu 'dropProperties' using the value grabbed from 'dropAccounts' but it's not even echoing in my PHP script.

propertyID.php:

<?php

if($_POST['id'])
{
    $accountID = $_POST['id'];
    echo $accountID;
}

?>

I was following the tutorial found here: http://www.9lessons.info/2010/08/dynamic-dependent-select-box-using.html

  • 写回答

3条回答 默认 最新

  • weixin_33734785 2014-05-08 18:50
    关注

    According to what I see the problem is the following:

    var dataString = 'id='+ id;
    data: dataString
    

    You need to send the data in the following way;

    data : {
             'id' : id
           }
    

    And it should send you the data in the correct format.

    EDIT: I'm adding this to clarify how your code would look.

    <script type="text/javascript">
        $(document).ready(function()
        {
            $(".dropAccounts").change(function()
            {
                var id=$(this).val();
    
                $.ajax
                ({
                    type: "POST",
                    url: "propertyID.php",
                    data: {
                             'id' : id
                    },
                    cache: false,
                    success: function(html)
                    {
                        $(".dropProperties").html(html);
                    } 
                });
    
            });
    
        });
    </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容