Welcome Guest, Not a member yet? Register   Sign In
Using one delete function for a post jquery request of php request
#1

[eluser]xtremer360[/eluser]
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.

Code:
<?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

Code:
/* 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');
});




Theme © iAndrew 2016 - Forum software by © MyBB