【问题标题】:Look for where code fails寻找代码失败的地方
【发布时间】:2012-08-24 15:04:43
【问题描述】:

查看此 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 而没有在服务器端设置数据。

我怎样才能找到失败的地方?有什么方法或工具可以完成这项工作吗?

【问题讨论】:

  • 在 PHP 中,使用var_dump($_POST);。这将告诉您通过 POST 传递给 PHP 脚本的内容。
  • 什么“不起作用”。代码当前做什么?
  • 仅供参考,它是 HTML 代码,并且您的
  • @DocRoms:很确定它在 HTML5 中有效。
  • @DocRoms 嗯,它是 HTML5 有效的,但我使用 Modernizr 等其他库在旧浏览器和我们可爱的 IE 中完成这项工作;)而且我将此属性编写为 CSS,注意样式标记,然后我认为这是纯 CSS 而不是输入标签属性,所以无论如何都应该可以工作,感谢您的建议

标签: php javascript jquery methods


【解决方案1】:

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

因此,$(location).attr('href', "&lt;?php echo site_url(); ?&gt;"); 将在 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(); ?>");
});

【讨论】:

  • 感谢您和您的建议,我现在可以看到它失败的地方,是一列的数据库问题。我正在阅读有关回调的内容,但直到现在才知道如何在此配置文件中使用。非常感谢您和其他所有人
猜你喜欢
  • 1970-01-01
  • 2016-07-30
  • 1970-01-01
  • 2011-05-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多