2016-02-05 65 views
0

我正在使用ajax和CodeIgniter實現一個評論框。使用Ajax和Codeigniter的評論系統

我想要一個腳本,其中登錄的用戶評論,並且Ajax發送user_idpost到控制器,評論被添加到MySQL數據庫和評論列表然後刷新。 下面的這段代碼正在做的功能只有我希望它使用ajax完成。 這裏是scenery.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'); 


} 


    } 
+4

請告訴我你的代碼:) – sukalogika

+4

你嘗試過什麼嗎?我們不能爲你做所有事情 – Gwendal

+0

這可能會幫助你開始:http://stackoverflow.com/a/28604136/3778613但是像其他人說的,我們不能爲你做。你必須顯示一些代碼:) – AdrienXL

回答

1

你可以先讓它沒有AJAX只是爲了讓去的功能。 也不要發送用戶標識,因爲您應該已經在服務器端有該用戶標識,只需發送帖子標識。

2

如果用戶登錄,可能是將用戶的數據存儲在會話變量中,因此您可以從其會話數據中獲取用戶的ID(實際上,您不應該從前端接受此信息,因爲用戶可以很容易地改變他們的UID的價值,假裝是使用Chrome Developer Tools或Firebug的人)。一旦他們已登錄,您可以使用jQuery的$就法提交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>'); 
     } 
    } 
}); 

在笨後端,你會添加一個方法到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)); 
} 
+0

你好,我可以看到表格本身 –