【问题标题】:Commenting system with Ajax and Codeigniter使用 Ajax 和 Codeigniter 的评论系统
【发布时间】:2016-05-15 02:21:15
【问题描述】:

我正在使用 ajax 和 CodeIgniter 实现一个评论框。

我想要一个脚本,其中登录用户 cmets,Ajax 将 user_idpost 发送到控制器,评论被添加到 MySQL 数据库,然后刷新评论列表。 下面的这段代码正在做的只是我希望它使用 ajax 完成的功能。 这是我的一部分风景.php

  <?php 
$scenery_id=$scenery1['scenery_id'];

 echo form_open(('display_scenery/add_comment/'.$scenery_id)); ?>

 <div class="input-group" ><!-- input group starts-->

  <input type="text" class="form-control" id ="Comment" name ="Comment"  placeholder="Comment on Scenery..." maxlength="300" size= "70" required> 
  <input type ="hidden" name= "Scenery_id" value= " <?php echo $scenery_id?>" />

   <button type="submit"  id = "submit"class="btn btn-info regibutton" >Post</button>


    </div>

 </form>
  <hr/>
   <div id = "comment-box">
 <?php
  if ($comment==NULL){
 //if no scenery comment echo disclaimer
         echo " <ul style = ' margin-left: 0px;padding-left: 0px;'> <li style = 'list-style: none; background-color: #fff; padding : 5px 5px 5px 10px; margin: 5px 5px 5px 5px'>";
      echo " No scenery Comments";  
       echo "</li>
 </ul>"; 
} else{

   foreach ($comment as $row){
 // if the  comments are availabe echo  them
    echo " <ul style = ' margin-left: 0px;padding-left: 0px;'> <li style = 'list-style: none; background-color: #fff; padding : 10px 5px 5px 10px; margin: 5px 5px 5px 5px'>";
  echo $row->Comment;
   echo "<br/>";
     echo "<p style='font-size: 11px; color:#333; padding-top: 5px;'>".date(" D d M Y - H:i:s ",strtotime($row->Date_posted))."By - ". $row->Username. " </p>";
     echo $row->Date_added;

     echo "</li>
 </ul>";
 }

 }
   }
  ?>
  </div>
   </div>
  <br>
 <br>

这是我的控制器 display_scenery.php

    public function add_comment(){

$this->load->library('form_validation'); 
    $session_data = $this->session->userdata('logged_in');
        $User_id= $session_data['User_id'];
    $scenery_id = $_POST['Scenery_id'];
    $Comment=$_POST['Comment'];

 $this->form_validation->set_rules('Comment', 'Comment', 'trim|required');
  if($this->form_validation->run() == FALSE)
    {
        ///$error=   form_error('Comment');
        $this-> session->set_flashdata('error',  form_error('Comment'));    
        redirect ('scenery', 'refresh');
}
else {
    //loads the model image_display then redirects to scenery page
  $this-> image_display->add_comment( $scenery_id,     $Comment,$User);             
  redirect ('scenery', 'refresh');


}


   }

【问题讨论】:

  • 请给我看看你的代码:)
  • 你试过了吗?我们不能为您做所有事情
  • 这可能会帮助您开始:stackoverflow.com/a/28604136/3778613 但就像其他人说的那样,我们不能为您做这件事。你必须显示一些代码:)

标签: php mysql ajax codeigniter


【解决方案1】:

如果用户已登录,大概您将用户的数据存储在会话变量中,因此您可以从他们的会话数据中获取用户的 ID(事实上,您不应该从前端接受此信息,因为用户可以使用 Chrome 开发者工具或 Firebug 轻松更改其 UID 的值以伪装成其他人)。登录后,您可以使用 jQuery 的 $.ajax 方法提交 AJAX 查询:

$.ajax({
    // Submitting to Controller_name->submit_comment() on your site
    'url': 'https://www.example.com/controller_name/submit_comment',
    // Submit as a POST request
    'type': 'POST',
    // comment_text should be a variable containing
    // the text of the comment
    'data': 
    {
        'comment_text': comment_text
    },
    // Controller method will return a JSON object
    'dataType': 'json',
    // Success method for response
    'success': function (response)
    {
        // If success, display comment. This is all coming
        // from the PHP code below.
        if (response.status === true)
        {
            $('#comments').append('<div class="comment"><span class="user-id">' + response.comment.user_id + '</span> at <span class="timestamp">' + response.comment.timestamp + '</span></br />' + response.comment.text + '</div>');
        }
        // Else failure, display error
        else
        {
            $('#comments').append('<div class="comment-error">' + response.error + '</div>');
        }
    }
});

在 CodeIgniter 后端,您将向 Controller_name 控制器添加一个名为 submit_comment 的方法:

public function submit_comment()
{
    $response = array(
        'status' => FALSE,
    );

    // Get the various pieces of data that are needed
    // to store the comment - text, user ID, timestamp of comment.
    $comment = array(
        // Get user ID from session data
        'user_id' => $this->session->userdata('user_id'),
        // Get comment from POST data
        'text' => $this->input->post('comment'),
        // Set timestamp to current epoch time
        'timestamp' => time()
    );

    // Do your validation and database query to save the comment here.
    // Store the result in a variable called $comment_posted
    // (TRUE for success, FALSE for failure).

    // If data validation/database query succeed:
    if ($comment_posted)
    {
        // Set status to true to indicate success.
        $response['status'] = TRUE;
        // Rewrite timestamp to date/time string since humans don't speak epoch.
        $comment['timestamp'] = date('m/d/Y g:ia', $comment['timestamp']);
        // Include comment object in body of response.
        $response['comment'] = $comment;
    }
    // Else the validation/query failed, return an error message.
    else
    {
        // Return a useful error message.
        $response['error'] = 'Some useful error message here.';
    }

    // Print the response array as a JSON-encoded string,
    // which will be consumed by the success method in
    // the Javascript code above.
    die(json_encode($response));
}

【讨论】:

  • 你好,我也可以看看表格本身吗
【解决方案2】:

您可以先在不使用 AJAX 的情况下创建它,以使功能正常运行。 也不要发送用户 ID,因为您应该已经在服务器端拥有该用户 ID,只需发送帖子 ID。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-24
    • 1970-01-01
    相关资源
    最近更新 更多