![]() |
ajax call on success redirect page from controller loaded in place of form - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: ajax call on success redirect page from controller loaded in place of form (/showthread.php?tid=49027) |
ajax call on success redirect page from controller loaded in place of form - El Forum - 02-06-2012 [eluser]axmed[/eluser] Hello Everyone i have this problem cant find a solution for it.this is how it goes, i have Form using Ajax i would call a controller and it will respond by sending back the form with the validation errors, but when no errors are found and a redirect is made by the controller the Ajax call loads the redirected page in the place of the form, leaving me with a page in a page (header header footer footer). so how can i handle Calling the redirect() function in an AJAX call to go to another page. here is the code jquery: Code: $("form.user_form").live("submit",function(event) { PHP Controller Code: function get_password() side notes rest library for template, Autoform is to create a form. Any advices? Thanks ajax call on success redirect page from controller loaded in place of form - El Forum - 02-06-2012 [eluser]PhilTem[/eluser] Maybe this snippet from my code can help you Code: $('form#ajax_form :submit').click(function() { What it does is fairly simple: Attached to each submit button of a form (I tend to have one for "Save and close" and one for "Apply" for which the first does a redirect) I have an AJAX-call. This goes to the same page we're on but for the one call (AJAX) it will only produce a redirect if the "save and close" button was pressed (So it checks if there is a html-attribute called "redirect" for the button). The buttons are created with Code: <input type="submit" name="smt_submit" class="btn primary" redirect="http://example.com/thisToRedirectTo.php" value="Save changes" /> On another way (and to cut a probably long story short): You have to check within your controller, whether you want to perform a redirect (on an ajax-call) and then send the string to redirect to to your ajax success-function. Then just use Code: \window\.\location\.href = theRedirect; // without the \ but I had to add them since the forum stripped them and with pure magic you have a redirect working. My example won't work out of the box but should guide you towards a functional script ![]() ajax call on success redirect page from controller loaded in place of form - El Forum - 02-06-2012 [eluser]pickupman[/eluser] By using the third parameter of load->view you can get the html from the page. Code: if ( !$this->input->is_ajax_request()) ajax call on success redirect page from controller loaded in place of form - El Forum - 02-06-2012 [eluser]axmed[/eluser] this problem driving me mad since i know so little about ajax, i tried using http headers and nothing ajax continued to ignore the redirect and load it in the div, so i tried what PhilTem said, is this the correct way to do it? will it be supported by browsers or cause any error. here is the fix: jquery Code: $("form.user_form").live("submit",function(event) { and in the Controller Code: function _show_message($message, $state = true, $redirect ='') ajax call on success redirect page from controller loaded in place of form - El Forum - 02-06-2012 [eluser]Aken[/eluser] Yes, that is a more appropriate way to do it. You cannot use a redirect in your PHP code and expect Javascript to enact it - just not the way the two languages work together. See this StackOverflow article about JS redirects for more info, if you haven't seen it already. ajax call on success redirect page from controller loaded in place of form - El Forum - 02-06-2012 [eluser]axmed[/eluser] Thanks guys for all the help, especially PhilTem for the solution ![]() |