在codeigniter中通过jquery删除后刷新页面

时间:2013-08-21 07:14:37

标签: php

我认为从数据库中获取所有帖子,如下所示: -

<ol class="timeline2 clear">
  <li class="spine">

  </li>

  <?php 
  $counter=0;
  foreach ($response as $row) { 
  if($counter % 2 == 0){$class= "left";} else $class="right";
  ?>

   <li class="<?=$class ?>">
    <i class="pointer"></i>
    <div class="unit">

      <!-- Story -->
      <div class="storyUnit">
        <div class="imageUnit">
         <? if (empty($row->pic)) { ?>
          <a href="#"><img width="32" height="32" alt="" src="images/nopic.png"></a>
          <? } else  { ?>
          <a href="#"><img width="32" height="32" alt="" src="uploads/<?php echo $row->pic; ?>"></a>
          <div id="delpost" style="float:left"><a href="#" id="deletepost" onClick="delete_post('<?=$row->ev_id;?>');">X</a></div>
          <? } ?>
          <div class="imageUnit-content">
            <h4><a href="./home/myaccount/<?php echo $row->id; ?>"><?php echo $row->fullname; ?></a></h4>
            <p><?php echo $row->ev_date ?></p>
          </div>

        </div>

        <p> <?php echo $row->ev_text; ?><br />
        <? if (!empty($row->ev_pic)) { ?>
        <img src="uploads/<?php echo $row->ev_pic ?>" width="250" height="250"</p>
        <? } ?></p>

      </div>
      <!-- / Story -->

      <!-- Units -->
      <ol class="storyActions">


      </ol>
      <!-- / Units -->

    </div>
  </li>

   <?php $counter++; } ?>
   <div class="clear"></div>
</ol>

然后我不会删除任何帖子,所以我点击: -

<div id="delpost" style="float:left"><a href="#" id="deletepost" onClick="delete_post('<?=$row->ev_id;?>');">X</a></div>

函数delete_post是调用控制器删除帖子的jquery: -

function delete_post( ev_id )
{   
    var message_confirm =confirm("are you sure?");
    if (message_confirm==true)
       {
    $.ajax({
        url: "home/deletePostInProfile",
        type: "POST",
        data: {ev_id : ev_id },
        dataType: "json",
        success: function(data)
        {
            if(data.process == "ok")
            {
                $("#post_row"+ev_id).remove();      
            }

        }  
    });
       }
       }

所以当调用函数和删除的帖子时,页面刷新并转到我的主页。

控制器是: -

public function deletePostInProfile() {

$this->load->model('blog');
$ev_id=intval($_POST['ev_id']);

$result = $this->blog->deletePost($ev_id);

}

为什么刷新页面并转到主页?

2 个答案:

答案 0 :(得分:1)

如果您想在删除帖子后重新加载页面,请执行以下操作:

function delete_post( ev_id )
{
    var message_confirm =confirm("are you sure?");
    if (message_confirm==true)
    {
        $.ajax({
            url: "home/deletePostInProfile",
            type: "POST",
            data: {ev_id : ev_id },
            dataType: "json",
            success: function(data)
            {
                if(data.process == "ok")
                {
                    $("#post_row"+ev_id).remove();
                    window.location.href = "http://your.url";
                }

            }
        });
    }
}

答案 1 :(得分:0)

使用window.location.reload()

成功删除后可以重新加载页面
function delete_post( ev_id )
{   
    var message_confirm =confirm("are you sure?");
    if (message_confirm==true)
       {
    $.ajax({
        url: "home/deletePostInProfile",
        type: "POST",
        data: {ev_id : ev_id },
        dataType: "json",
        success: function(data)
        {
            if(data.process == "ok")
            {
                $("#post_row"+ev_id).remove(); 
                window.location.reload();     
            }

        }  
    });
       }
       }