使用codeigniter方法的Ajax发布请求

时间:2017-04-17 14:16:50

标签: jquery ajax codeigniter

尝试发送此帖子请求,但在点击附加时没有响应"回复"按钮。控制台日志中也没有报告错误。当"回复"点击链接,textarea附加到父评论和按钮,"回复"出现在textarea下面。单击此按钮时,我希望发送ajax post请求:

jQuery的:

$("a#reply").one("click", function() {
    var comCode = $(this).attr("name");
    var parent = $(this).parent();

    parent.append("<br /><textarea class='form-text' name='new-reply' id='new-reply' required='required'></textarea><input type='hidden' name='child_table' id='child_table' value='child' /><input type='hidden' name='code' id='code' value='"+comCode+"' /><input type='submit' class='form-submit' id='form-reply' name='new_reply' value='Reply' />")

});

$("#form-reply").click(function(){
var child_table = $("#child_table").val();
var comment = $("#new-reply").val();
var code = $("#code").val();

$.ajax({
    type: "POST",
    url: "/new_login/index.php/user_authentication/check_comments",
    data: {
        new_reply : 1,
        child_table : child_table,
        new_replies : comment,
        code: code
    },
    success: function(data){
        alert('Success!');
    }
    });
});

PHP:

  public function check_comments()
  {
  //  New Reply
    if(isset($_POST['new_reply']))
    {
      $new_reply_table = $_POST['child_table'];
      $new_reply_name = $this->session->userdata['logged_in']['username'];
      $new_reply_text = $_POST['new_replies'];
      $new_reply_date = date('Y-m-d H:i:s');
      $new_reply_code = $_POST['code'];

      if(isset($new_reply_text))
      {
        $data = array(
          'user' => $new_reply_name,
          'text' => $new_reply_text,
          'date' => $new_reply_date,
          'par_code' => $new_reply_code
        );
        $this->user_authentication_model->check_comments($new_reply_table, $data);
      }
        $data['email'] = $this->user_authentication_model->show_user_email();
        $data['get_comments'] = $this->get_comments();
        $data['get_total'] = $this->user_authentication_model->get_total();
        $this->load->view('user_page', $data);
    }
  }        

                 ...

1 个答案:

答案 0 :(得分:0)

您应该在控制器中回显ajax调用的代码

@Override
public boolean equals(Object other) {
    return other == this; // identity equality
}

}

在你的ajax中

public function check_comments()
{
//  New Reply
if(isset($_POST['new_reply']))
{
  $new_reply_table = $_POST['child_table'];
  $new_reply_name = $this->session->userdata['logged_in']['username'];
  $new_reply_text = $_POST['new_replies'];
  $new_reply_date = date('Y-m-d H:i:s');
  $new_reply_code = $_POST['code'];

  if(isset($new_reply_text))
  {
    $data = array(
      'user' => $new_reply_name,
      'text' => $new_reply_text,
      'date' => $new_reply_date,
      'par_code' => $new_reply_code
    );
    $this->user_authentication_model->check_comments($new_reply_table, $data);
  }
    $data['email'] = $this->user_authentication_model->show_user_email();
    $data['get_comments'] = $this->get_comments();
    $data['get_total'] = $this->user_authentication_model->get_total();
    echo  json_decode($data); // dont load view
}