【问题标题】:CI Post to ControllerCI 发布到控制器
【发布时间】:2014-04-29 18:38:23
【问题描述】:

我今天在构建 CI 时有点做噩梦,我希望聪明的 stck 社区可以帮助我 :)

我在 CI 安装中有一个名为 comment_m.php (model)comment_v.php (view)comment.php (controller) 的模型、视图和控制器。

我面临的问题是它似乎没有进入控制器方法,因此没有插入到数据库中。

页面的URLhttp://localhost/comment/1234,如果这有什么不同的话,出于某种原因,我没有收到任何错误,当我尝试使用log_message() 时,它没有写入。

谁能看到我在这里所做的任何明显或确实愚蠢的事情,这可能会导致问题?

代码


PHP / HTML

<?php
$cattributes = array('id' => 'newComment', 'enctype'=>"multipart/form-data");
echo form_open('comment/saveComment', $cattributes);
?>
 <ul class="pull-left nav navbar-nav bar-buttons">
  <li class="camera-button" id="c_cancelLi">
   <a href="#cancel" title="Cancel this action" id="c_cancelBtn"><?=lang('board.cancel-btn');?></a>
  </li>
  <li class="link-button" id="c_photoLi">
   <input class="btn btn-default" id="c_ImgBtn" name="cmmt_image" type="file" class="filestyle">
  </li>
  <li class="link-button" id="c_linkLi"><a href="#link" title="Post a link" id="cLinkBtn"><span class="btn-icon btn-icon-link"></span></a></li>
 </ul>
 <textarea style="display: none" name="cmmt_text" id="c_cmmt_text"></textarea>
 <input type="hidden" name="cmmt_location_name" id="c_cmmt_location_name" value="PASS Online">
 <input type="hidden" name="cmmt_latitude" id="c_cmmt_latitude" value="0">
 <input type="hidden" name="cmmt_longitude" id="c_cmmt_longitude" value="0">
 <input type="hidden" name="cmmt_user_id" id="c_cmmt_user_id" value="<?=$user_id;?>">
 <input type="hidden" name="cmmt_type" id="c_cmmt_type" value="0">
 <input type="hidden" name="cmmt_link" id="c_cmmt_link" value="">
 <input type="hidden" name="cmmt_link_title" id="c_cmmt_link_title" value="">
 <input type="hidden" name="cmmt_post_id" value="<?=$pid;?>"/>
 <ul class="pull-right nav navbar-nav bar-buttons">
  <li class="send-button">
   <input type="submit" class="je-send_button" id="cmmtSendPost" value="<?=lang('board.send');?>">
  </li>
 </ul>
 <section class="gap">&nbsp;</section>
<?php
echo form_close();
?>

JQuery

$('#newComment').submit(function(cmmts) {
    //$('#hud-overlay').show();
    var comment = $('#cmmtTextEntry').html();
    $('#c_cmmt_text').val(comment);
    var formObj = $(this);
    var formURL = formObj.attr("action");
    var formData = new FormData(this);
    $.ajax({
        url: formURL,
        type: 'POST',
        data:  formData,
        mimeType:"multipart/form-data",
        contentType: false,
        cache: false,
        dataType: "HTML",
        processData:false,
        success: function(data, textStatus, jqXHR) {
            window.location.reload(true);
        },
        error: function(jqXHR, textStatus, errorThrown) {
            console.log(errorThrown);
            console.log(jqXHR);
            console.log(textStatus);
        }
    });
    cmmts.preventDefault(); //Prevent Default action.
    //f.unbind();
});

Controller

public function saveComment($_POST) {
    $postId = $_POST['cmmt_post_id'];
    $type = $_POST['cmmt_type'];
    if($type == 0) {
        $this->comment_m->commentThought($_POST);
        redirect('board');
    } elseif($type == 1) {
        //image
        $this->comment_m->cmmtImage($_POST);
        log_message('info', 'inside of cmmt if type 1.');
        redirect('comment/'.$postId);
    } elseif($type == 3) {
        //link
        $this->comment_m->cmmtLink($_POST);
        log_message('info', 'inside of cmmt if type 3.');
        redirect('comment/'.$postId);
    } else {
        log_message('info', 'inside of cmmt if else just returning.');
        return;
    }
}

Model

public function commentThought() {
    $media = $this->input->post('cmmt_user_id').'-'.substr($sha1,0,25).'.jpg';
    $now = microtime(true);
    $cmmtText = $this->input->post('cmmt_text');
    //Regex Functions
    $p1 = '~<span contenteditable=\\"false\\" class=\\"atwho-view-flag atwho-view-flag-@\\">|<span contenteditable=\\"false\\" class=\\"atwho-view-flag atwho-view-flag-#\\">|</span>|<span>|<span contenteditable=\\"false\\">|&nbsp;~';
    $r1 = '';
    $start = preg_replace($p1, $r1, $cmmtText);
    $users = preg_replace("~(<var data-type=\"user\" class=\"userHighlight\" id=\"(.*?)\">)(.*?)(</var>)~", "<_link>$2|$3</_link> ", $start);
    $tags = preg_replace("~(<var data-type=\"tag\" class=\"tagHighlight\" id=\"(.*?)\">)#(.*?)(</var>)~", "<_link>tag://$3|#$3</_link> ", $users);
    $last = preg_replace("~(^|\\s)#(\\w*[a-zA-Z_]+\\w*)~", " <_link>tag://$2|#$2</_link> ", $tags);
    //End

    $data = array(
        'cmmt_type' => 0,
        'cmmt_post_id' => $this->input->post('cmmt_post_id'),
        'cmmt_user_id' => $this->input->post('cmmt_user_id'),
        'cmmt_latitude' => $this->input->post('cmmt_latitude'),
        'cmmt_longitude' => $this->input->post('cmmt_longitude'),
        'cmmt_location_name' => $this->input->post('cmmt_location_name'),
        'cmmt_text' => $last,
        'cmmt_media_url' => $media,
        'cmmt_deleted' => 0,
        'cmmt_updated' => $now,
        'cmmt_posted_date' => $now,
        'cmmt_language_id' => 1,
    );

    $this->db->insert('PAS_Comment', $data);

    return true;
}

【问题讨论】:

    标签: php jquery codeigniter activerecord


    【解决方案1】:

    如果你的 Controller 没有被命中,你可能有路由问题。

    如果您的类/方法拼写错误,或与实际不相关 命名约定,使用路由文件制作。

    application/config/routes.php

    $route['comment/save'] = 'class/method';
    

    错误报告

    application/config/confg.php 中,确保您的消息已启用(仅限开发模式)

    $config['log_threshold'] = 1;
    

    编码类型

    省去一些步骤,使用form_open_multipart('comment/save', array('id'=&gt;''))


    访问输入($_POST、$_GET、$_FILE)

    使用输入库$this-&gt;input-&gt;post,因为它会为您检索全局变量,如果您启用了 XSS,它会在非常基本的级别为您清理输入字段。


    表单验证

    始终验证用户输入,即使他们是管理员。 再一次不要使用诸如$_POST 之类的全局变量,输入库会有所保护 针对XSS,但是如果您的字段是整数类型,请使用form_validation 库将其验证为一个。

    【讨论】:

    • 太棒了!!!谢谢,这是我认为我曾经有过的最全面的答案,我非常感谢您的指示和解决方案。
    • 欢迎您@贾斯汀。
    猜你喜欢
    • 1970-01-01
    • 2013-07-03
    • 2016-05-28
    • 2012-01-25
    • 2013-12-15
    • 2014-12-17
    • 2013-08-10
    • 2016-10-03
    • 2016-07-05
    相关资源
    最近更新 更多