使用一个删除函数为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');
});

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) As far as "displaying a message" - the view-itself could be ready for a 'message' if one exists. Bringing us back to...

2) Can you have your delete function return the message you want displayed? Your AJAX approach will ignore this message while your View will display it...

3) I agree that your 'Controller delete function' should 'finish' with different outcomes based on whether the request is AJAX or not. I like what @Skaparate (answered Aug 30 at 18:37) was doing with adding: js:1 In your delete function, you could use this in a simple conditional:

if js = 1
    header('HTTP/1.1 200');
else
    call view and include/pass-in the 'message'