douzhimei8259 2013-08-30 18:15
浏览 33
已采纳

使用一个删除函数为php请求的post jquery请求

I'm looking to find out how I can accomplish this next task. I have a controller that loads a view with a table that lists pages in my database. In every row that is made in the table there is a spot for a icon that when clicked will do one of two things.

If the user does not have javascript enabled:

  • Clicking on the icon will redirect to the delete function in the controller with id of the page as a paramter
  • Delete controller function will run delete function in model sending page id to delete model function
  • Delete function in model will delete the page from the database and when returned back to page controller delete function it will redirect back to index function to show list of pages table again.
  • After redirect it will display a title and message as to a success/fail.

If the user does have javascript enabled:

  • Send a post to the delete function in controller with jquery post method with the data page id
  • Delete controller function will run delete function in model sending page id to delete model function
  • Delete function in model will delete the page from the database and when returned back to page controller delete function it create a message array for the json object to return to the success function of the post request.
  • A message with my pnotify plugin will create a message that is formed from that json object and display it to the user

What I would like to know is with doing this how to properly accommodate for these two scenarios? I have started attempting this but would like to some clarification if I have made a mistake so far.

<?php
// Controller delete function
public function delete($content_page_id)
{
    if (isset($content_page_id) && is_numeric($content_page_id))
    {
        $content_page_data = $this->content_page->get($content_page_id);
        if (!empty($content_page_data))
        {
            //update is ran instead of delete to accompodate 
            //for the soft delete functionality
            $this->content_page->update('status_id', 3);
            if ($this->input->is_ajax_request())
            {
               //return json data array
            }
        }
    }
}
?>

Global JS file to be used for multiple tables with delete buttons

/* Delete Item */
$('.delete').click(function(event) { 
    event.preventDefault();
    var item_id = $(this).attr('rel');
    $.post(<?php echo current_url(); ?>'delete', { item_id : item_id }, function(data)  
    {
        if (data.success)
        {
            var anSelected = fnGetSelected( oTable );
            oTable.fnDeleteRow( anSelected[0] );
        }
    }, 'json');
});
  • 写回答

2条回答 默认 最新

  • dousong2023 2013-08-30 18:37
    关注

    I think that you should have two functions in PHP:

    public function delete($content_page_id) {
      // Your delete code
    
      return "a string without format";
    }
    
    public function deleteAjax($content_page_id) {
      return json_encode($this->delete($content_page_id));
    }
    

    So, when the user has JS enabled, you call deleteAjax passing a parameter in your $.post function to let PHP know that JS is enabled:

    $.post(<?php echo current_url(); ?>'delete', { item_id : item_id, js: 1 }, function(data)  
    {
        if (data.success)
        {
            var anSelected = fnGetSelected( oTable );
            oTable.fnDeleteRow( anSelected[0] );
        }
    }, 'json');
    

    And if JS is disabled, call the other function. You should use an AJAX specialized controller instead a function in the same class.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改