2014-07-04 53 views
0

美好的一天。我是CodeIgniter的新手,我試圖在刪除記錄後使用flashdata顯示「成功」的消息。我使用jQuery來顯示我的數據。在codeigniter中刪除記錄後,flashdata無法正常工作

這裏是Model頁面的代碼:

function delete_post($postID) 
{ 
    $this->db->where('post_id', $postID); 
    $this->db->delete('khanposts'); 
} 

Controller頁面的代碼:View頁的

function deletepost($postID) 
{ 
    $this->khanpost->delete_post($postID); 
    $this->session->set_flashdata('msg', 'Record Deleted!'); 
    redirect(base_url().'khanposts/index/'); 
} 

代碼:

foreach ($posts as $row){ 
echo '<tr><td><a href="' .base_url(). 'khanposts/deletepost/' .$row['post_id']. '"><i>Delete</i></a></td></tr>'; 
} 
<script> 
$(document).ready(function() { 
$('.delmsg').hide(); 
<?php if($this->session->flashdata('msg')){ ?> 
$('.delmsg').html('<?php echo $this->session->flashdata('msg'); ?>').show(); 
}); 
<?php } ?> 
</script> 

記錄被刪除,但我不在刪除後不會看到任何消息。我做錯了嗎?任何解決方案都會有幫助。 TNX。

+0

當您查看頁面源代碼時,是否可以看到錯誤消息? – Craig

+0

@克雷格,不,我沒有看到任何消息。但是記錄被刪除。 –

回答

0

第一,但可能不會有所作爲,但是,我已經扭轉了第二到最後,三到最後一行在您的視圖代碼:

foreach ($posts as $row){ 
    echo '<tr><td><a href="' .base_url(). 'khanposts/deletepost/' .$row['post_id']. '"><i>Delete</i></a></td></tr>'; 
} 

<script> 
    $(document).ready(function() { 
     $('.delmsg').hide(); 
     <?php if($this->session->flashdata('msg')){ ?> 
      $('.delmsg').html('<?php echo $this->session->flashdata('msg'); ?>').show(); 
     <?php } ?> 
    }); 
</script> 

這似乎是與問題加載會話庫,我猜想。因此,請檢查是否在application/config/autoload.php配置文件中加載了會話類,方法是將'session'添加到$ autoload ['libraries']數組中:$autoload['libraries'] = array('session'); ...或在控制器中使用$this->load->library('session')

如果仍然沒有工作,確保你在根index.php文件「的發展模式」和你的開發情況是這樣的:

case 'development': 
     error_reporting(E_ALL); 
     ini_set('display_errors', 1); 

...看看PHP可以爲您提供有關正在發生的任何其他信息。

祝你好運! :-)

0

您可以使用php而不是Jquery顯示錯誤消息。您可以像這樣使用

if($this->session->flashdata('msg')) 
{?> 
    <div class="delmsg"><?php echo $this->session->flashdata("msg"); ?></div> 

<?php }