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条)

报告相同问题?

悬赏问题

  • ¥15 springboot+vue 集成keycloak sso到阿里云
  • ¥15 win7系统进入桌面过一秒后突然黑屏
  • ¥30 backtrader对于期货交易的现金和资产计算的问题
  • ¥15 求C# .net4.8小报表工具
  • ¥15 安装虚拟机时出现问题
  • ¥15 Selenium+docker Chrome不能运行
  • ¥15 mac电脑,安装charles后无法正常抓包
  • ¥18 visio打开文件一直显示文件未找到
  • ¥15 请教一下,openwrt如何让同一usb储存设备拔插后设备符号不变?
  • ¥50 使用quartz框架进行分布式任务定时调度,启动了两个实例,但是只有一个实例参与调度,另外一个实例没有参与调度,不知道是为什么?请各位帮助看一下原因!!