Question

I am having a bit of a nightmare with a CI build today and I am hoping that the clever stck community can bail me out :)

I have a model, view and controller named comment_m.php (model), comment_v.php (view) and comment.php (controller) in CI installation.

The issue I am facing is that it doesn't seem to be getting inside the controller method and therefore is not inserting to the database.

The URL of the page is http://localhost/comment/1234 if that makes any difference, for some reason I am not getting any errors and when I try and use log_message() it is not writing.

Can anyone see anything obvious or indeed stupid that I am doing here which could be causing the problems?

The code


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;
}
Was it helpful?

Solution

If your Controller is not getting hit, you may have a routing problem.

If your Class/Method are spelt incorrectly, or don't relate to actual naming conventions, use the route file to make one.

application/config/routes.php

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

Error Reporting

Inside application/config/confg.php, make sure your messages are enabled(development mode only)

$config['log_threshold'] = 1;

Enctype

Save you some steps, use form_open_multipart('comment/save', array('id'=>''))


Accessing Inputs($_POST,$_GET,$_FILE)

Use the Input library $this->input->post as it will retrieve the Globals for you and at a very basic level sanitize the input fields for you if you have XSS enabled.


Form Validation

Always validate user inputs, even if they are admins. Again never use Globals such as $_POST, The input library will somewhat protect against XSS, however if your field is an integer type, validate it as one with the form_validation library.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top