-4

この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が原因でサーバー側にデータが設定されていないのかわかりません。

失敗した場所を見つけるにはどうすればよいですか?これを行うための方法やツールはありますか?

4

1 に答える 1

1

$.post非同期です。これは、バックグラウンドで実行されることを意味します。

したがって、POSTが終了する前に$(location).attr('href', "<?php echo site_url(); ?>");が実行されます。

$.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(); ?>");
});
于 2012-08-24T15:10:54.140 に答える