寻找代码失败的地方

时间:2012-08-24 15:04:43

标签: php javascript jquery methods

请参阅此HTML标记:

<input value="9961" name="c_id" id="c_id" type="hidden">
<input name="user_id" id="user_id" value="1" type="hidden">
<textarea id="comments" name="comments" style="width: 310px; resize: none; height: 75px"></textarea>

然后我在jQuery中编写这段代码,通过.post发送这些数据:

$("#dialog-form").dialog({
    autoOpen: false,
    height: 220,
    width: 350,
    resizable: false,
    modal: true,
    buttons: {
        "Ok": function () {
            if ($('#comments').val() != '') {
                $.post("<?php echo site_url('wall/comment') ?>", {
                    value: $("#comments").val(),
                    user_id: $('#user_id').val(),
                    c_id: $("#c_id").val(),
                    is_post: true
                });
                $(this).dialog("close");
                $(location).attr('href', "<?php echo site_url(); ?>");
            }
        },
        "Cancelar": function () {
            $(this).dialog("close");
        }
    },
    close: function () {
        $("#comments").val("");
    }
});

但由于某些原因不起作用,但是因为我使用.post方法,我无法找到它失败的意思,如果是jQuery或者它是服务器端部分。

修改 这是获取数据并运行基本上是INSERT的查询的PHP代码:

    public function comment() {
        role_or_die('wall', 'comment', site_url(), lang('wall:no_permissions'));

        $message = $this->input->post('value', TRUE);
        $post_id = $this->input->post('c_id', TRUE);
        $user_id = $this->input->post('user_id', TRUE);

        $this->load->library('user_agent');
        $device = "";

        if ($this->agent->is_browser()) {
            $device = $this->agent->browser();
        }

        if ($this->agent->is_mobile()) {
            $device = $this->agent->mobile();
        }

        if ($this->wall_comment_m->insert(array('friend_id' => $user_id, 'message' => $message, 'post_id' => $post_id, 'device' => $device))) {
            $this->session->set_flashdata('success', lang('message:comment_add_success'));
        } else {
            $this->session->set_flashdata('error', lang('message:comment_add_error'));
        }
    }

我无法看到生成的SQL是错误还是由于location.href而未在服务器端设置数据。

我怎样才能找到失败的地方?完成这项工作的任何方法或工具?

1 个答案:

答案 0 :(得分:1)

$.post是异步的。这意味着它在后台运行。

因此,$(location).attr('href', "<?php echo site_url(); ?>");将在 POST完成之前运行

您需要使用$.post的回调。

var that = this;
$.post("<?php echo site_url('wall/comment') ?>", {
    value: $("#comments").val(),
    user_id: $('#user_id').val(),
    c_id: $("#c_id").val(),
    is_post: true
}, function(){
    $(that).dialog("close");
    $(location).attr('href', "<?php echo site_url(); ?>");
});