[英]CodeIgniter - Retrieve data from MySQL and display without refreshing the page
目前,我正在嘗試使用CodeIgniter 在用戶輸入后顯示評論或帖子,並在不刷新頁面的情況下顯示它 。
我一直在尋找答案的幾天,但仍然找不到適合我項目的答案。
抱歉地說,我對使用CodeIgniter和JQuery還是陌生的。 因為該項目的目標是希望我們學習新的編程語言。
這是我的VIEW中的代碼,我提取了主要代碼
<?php
foreach ($comment as $comments) {
if ($comments['PostID'] == $stickynote['PostID']) {
?>
<div style="background-color: lightblue; border-radius: 5px 5px 5px 5px; margin-bottom: 5px; width: 70%; height: 80px">
<a class="pull-left" href="#">
<!-- Profile Picture -->
<img style='margin-top: 9px; margin-left: 5px; border-radius: 5px; padding-left: 5px;' class="media-object" id="sub-photo" src="../../images/<?php echo $profile ?>" alt="<?php echo $profile ?>" alt="...">
</a>
<div class="media-body">
<!-- Display Username -->
<h4 class="media-heading"><?php echo $comments['FirstName'] ?></h4>
<p style="width: 500px; word-wrap:break-word;">
<?php echo $comments['Description'] ?>
</p>
</div>
</div>
<?php
} <!-- End of IF statement -->
} <!-- End of FOR LOOP -->
?>
這是我的控制器
public function college_garden() {
$this -> load -> model('LoginModel');
if ($this -> session -> userdata('logged_in')) {
$session_data = $this -> session -> userdata('logged_in');
$data['username'] = $session_data['username'];
$data = array(
'comment' => $this -> CommentModel -> readComment($data['username'])
);
$this -> load -> view('include/header');
$this -> load -> view('college_garden', $data);
$this -> load -> view('include/footer');
} else {
redirect('main', 'refresh');
}
}
這是我的模特
public function readComment($studentID) {
$this -> db -> select('PostID, Description, FirstName, LastName, PostTime, Image');
$this -> db -> from('Comment');
$this -> db -> join('CollegeUser', 'CollegeUser.StudentID = Comment.StudentID');
$this -> db -> join('PhotoGallery', 'PhotoGallery.PhotoID = CollegeUser.PhotoID');
$this -> db -> order_by("Comment.PostTime", "desc");
return $this -> db -> get() -> result_array();
}
感謝您對我的幫助。 歡呼。
要提交表單而不刷新,您將需要使用ajax。
像這樣的東西:
$("form").on("submit", function(){
$.post("submit.php", $(this).serialize(), function(){
..your code to display comments/post..
});
return false;
});
顯示評論/帖子的部分完全由您決定,您可以執行另一個Ajax查詢服務器並獲取帖子,也可以只輸出$("textarea").val();
如果你很懶。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.