Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何更新a<;部门>;使用jQuery和CakePHP?_Php_Jquery_Ajax_Cakephp - Fatal编程技术网

如何更新a<;部门>;使用jQuery和CakePHP?

如何更新a<;部门>;使用jQuery和CakePHP?,php,jquery,ajax,cakephp,Php,Jquery,Ajax,Cakephp,我正在使用CakePHP开发一个小型web应用程序,在一个表单页面上有一个下拉列表来选择工作编号。我想使用jQuery根据下拉列表中选择的作业编号更新两个文本字段(我也愿意使用默认的ajax助手,但我没有太多的成功) 以下是我的jQuery代码片段: <script> $(document).ready(function() { $('#job_id').change(function() { $.post('/surveys/jobd

我正在使用CakePHP开发一个小型web应用程序,在一个表单页面上有一个下拉列表来选择工作编号。我想使用jQuery根据下拉列表中选择的作业编号更新两个文本字段(我也愿意使用默认的ajax助手,但我没有太多的成功)

以下是我的jQuery代码片段:

<script>
    $(document).ready(function() {
        $('#job_id').change(function() {
            $.post('/surveys/jobdetails', {id: $(this).attr('id')});
        })
        .change();
    });
</script>

我想将每个文本字段的值设置为从ajax调用返回的作业的对应值。有很多非常好的jQuery和CakePHP文档,但我还没有找到任何能够完全涵盖我所要做的事情的文档。有人能看出我做错了什么吗?有没有更好的方法使用ajax用CakePHP更新div?

现在,ajax请求似乎命中了“/surveys/jobdetails”URL,但对结果没有任何影响。您需要向AJAX请求添加回调,如下所示:

$(document).ready(function() {
    $('#job_id').change(function() {
        $.post('/surveys/jobdetails', {id: $(this).attr('id')},
        function(result) {
            $('#job_id').html(result);
        });
    })
    .change();
});
jQuery中还有一个名为的便利函数,它进一步简化了它,获取URL的内容并将其应用于所选元素:

$(document).ready(function() {
    $('#job_id').change(function() {
        $(this).load('/surveys/jobdetails', {id: $(this).attr('id')});
    })
    .change();
});

您的CakePHP控制器需要如下所示:

function jobdetails() {
    // get the data however you want
    // $_POST['id'] will have the job_id
    print json_encode(array(
        'jobtitle' => $jobtitle,
        'department'=>$dept
    ));
    exit;
}
然后,您需要向
$添加回调。post
将实际更新字段:

$(document).ready(function() {
    $('#job_id').change(function() {
        $.post('/surveys/jobdetails', {id: $(this).attr('id')}, function(json) {
            // now that we are in the callback,
            // the variable json is an object
            // with the values we passed above
            // so we can update the fields with the new values
            $('#jobtitle').val(json.jobtitle);
            $('#department').val(json.department);
        });
    })
    .change();
});
我还建议您使用Firebug之类的工具,以便查看AJAX请求的进度,并确保服务器返回您认为返回的内容。它使测试和调试任何与AJAX相关的东西变得更加容易


在我看来,这比输出整个DIV进行更新要优雅得多,但是如果你想这样做,你只需要使用jQuery来实现你想要的。

非常感谢-我最终使用了html(结果)函数更新我的div。非常感谢您的回复-我无法让json编码工作,但您的输入确实有帮助。我还安装并运行了Firebug,因此也感谢您的推荐。json_encode仅在版本>=5.2.0时才包含在PHP中,这可能就是原因。有许多软件包可以模仿它的功能,但我知道不必为它费心。祝你好运
$(document).ready(function() {
    $('#job_id').change(function() {
        $.post('/surveys/jobdetails', {id: $(this).attr('id')}, function(json) {
            // now that we are in the callback,
            // the variable json is an object
            // with the values we passed above
            // so we can update the fields with the new values
            $('#jobtitle').val(json.jobtitle);
            $('#department').val(json.department);
        });
    })
    .change();
});