Do I need a view for an animated JQuery ajax delete row? |
[eluser]gwerner[/eluser]
I'm new to codeigniter and prior to this I've been hand coding everything. I'm in the process of converting a fairly simple admin to codeigniter. I've been using an animated delete row when a user wants to remove an item from a list. Do I need a view for this in my controller if I'm using ajax to accomplish the delete? Essentially the user never leaves the main view if ajax is handling the delete. But, it seems like there could be issues of having a controller with no view? One problem that came up, what if someone types a random id that doesn't exist in the url? The page displays blank. To handle this I could just put a redirect in the view below the database call. If the user uses the delete link as intended this works perfectly. It just seems that a view might need to be added. Controller Code: // remove users controller Model Code: function remove_users($id) Ajax Code: $.ajax({
[eluser]PhilTem[/eluser]
You don't really need a separate view for the AJAX request. I'd recommend handling it this way: Have a controller and a method that deals with deleting one item at a time. 1) First, you want to check that the submitted ID is valid (i.e. exists in the DB or however you want to check) 2) Delete the entry 3) Check if it is an AJAX request or a standard HTTP browser request (some people may have JavaScript deactivated, we need to take account of this as well) 4a) If it is an AJAX request and you successfully deleted the row, just return (i.e. echo) "success", otherwise return "failure" 4b) No AJAX request? Display a "delete successful" view/page. 5) On you prior page (the on with the form), alter your jQuery code to check for the return value of the page (which in your case is "success" or "failure"). Depending on that, you will hide the row (for "success") and for "failure" you will want to display an error in your personal fashion ![]() That's how I handle AJAX requests most of the time (also keeping the JavaScript-deactivated community in mind ![]() If you need more help with this topic let me know!
[eluser]gwerner[/eluser]
Thanks, that makes sense. I just wasn't sure if the view was necessary.
[eluser]PhilTem[/eluser]
For the request itself it's not necessary. But you may of course put the above mentioned "success" and "failure" messages into a view even though I don't see any real benefit in doing so ![]()
[eluser]Aken[/eluser]
To expand on Phil's answer, a view is certainly not necessary, but you should pass any displayed information back through the Output class rather than echoing directly. There are also other methods for the Output class that might be handy for you. Check it out. http://ellislab.com/codeigniter/user-gui...utput.html Code: $result = 'success';
[eluser]Tim Post[/eluser]
As others have said, you really don't _need_ a view for that, but I've used a generic 'ajax' view as a catchall for one off odd requests that I need to build in. You *definitely* want to avoid using something like echo to write to the browser in a controller, as Aken noted. It expects certain elements to exist in the array passed to it when it's loaded, such as: - HTTP status code - Result [success or failure] - error code (if failure) - error message (if failure) - data[] array containing response data if there's anything to show I then stuck a function in MY_Controller that accepts a few arguments to load that particular view, which all of my controllers extend. It then sets the appropriate headers, and the view returns the appropriate JSON response. I used the view because the function loading them checks to see if debug_(uri)_ajax.php exists and loads it if it does, depending on the session. On larger projects, it just made sense so that all AJAX requests could expect the same thing, with the exception of a few that simply had to be a bit more complicated (proxying to a vendor supplied API).
[eluser]gwerner[/eluser]
I think what everyone is saying makes sense. Below is my updated code. This still doesn't prevent someone from just typing in the browser the link with an invalid id. If the user does they will simply receive a blank page. I could use a redirect for this, but maybe I'm missing something here? (And, I still haven't taken into consideration non-javascript users.) Delete Link Code: <a class="delete" href="/newaccess/users/remove/<?php echo $value['users_slug']; ?>">Delete</a> Controller - The user would actually be routing though the index method in my controller. The index method simple creates a list view of all users at this point. This view provides a delete link per row for each user. The controller below would only be accessed if the delete link is clicked. Code: // remove users controller Model Code: function remove_users($id) Ajax Code: $.ajax({
[eluser]CroNiX[/eluser]
I sure hope your user is authenticated and has permission to delete users. Otherwise, yeah, you could have a problem there.
[eluser]Tim Post[/eluser]
I think you have the basics of it down. You do need to check to make sure: - It is an AJAX request - The user has the right to do it I'd also really consider turning on the built in XSRF protection. Additionally, (nitpicking), it's generally nice to set the HTTP status code appropriately when returning the response. A 200 when something bad happened isn't really consistent. It doesn't really matter in this particular case, but it's a nice habit to get into.
[eluser]gwerner[/eluser]
Ok. Still makes sense. Yes, the user must be authenticated and have privileges otherwise no access and they are taken to a login panel. So, I've got that covered. I didn't relaize you can check if the request is an AJAX request. Makes sense that you can though. One last question. What is the best practice for handling the request if it is not AJAX? Considering I didn't even know you can check for this I'm curious what might be the best way to handle non-AJAX requests. My first inclination would to simply redirect or do nothing since the view the user would be on is the index. The controller below is only accessed if the user clicks the delete link or type in the URL. If they click the link all should be well. If they try to type it in then it should just do nothing. That's what the link is for? Updated controller with AJAX check Code: // remove users controller |
Welcome Guest, Not a member yet? Register Sign In |