Handling redirects with Codeigniter and jQuery mobile? - codeigniter

I'm building a mobile web app with jQuery mobile and Codeigniter & TankAuth. When a user logs in or out, they are redirected from the TankAuth login/logout controller to a page of my choice. The problem is that while the ajax navigation loads the page in question, the URL to the TankAuth controller stays the same.
There is quite a lot of code involved, so I will describe it the best I can:
Consider the variable $this->session->flashdata('prev_page') is page1.
The TankAuth login controller is called first time via auth/login, loads login form view.
The form is submitted to the same controller which then processes it and redirects to necessary page:
if ($this->tank_auth->login(
$this->form_validation->set_value('login'),
$this->form_validation->set_value('password'),
$this->form_validation->set_value('remember'),
$data['login_by_username'],
$data['login_by_email'])) {// success
redirect($this->session->flashdata('prev_page'));
}
jQuery mobile loads page1 via AJAX but the URL remains as auth/login.
How do I fix this? Please let me know if you need any more info.

If my understanding about your problem is correct, you are using ajax for authenticating mobile users and normal redirect method for big screen users.
If that's the situation, then this will do the magic:
if ($this->tank_auth->login(
$this->form_validation->set_value('login'),
$this->form_validation->set_value('password'),
$this->form_validation->set_value('remember'),
$data['login_by_username'],
$data['login_by_email']))
{// success
$this->load->library('user_agent');
if($this->agent->is_mobile()) {
echo $this->session->flashdata('prev_page');
//Now this value 'll reach ajax when authentication is success.
//Inside Ajax:
//success: function(data) {
// validate data for checking if user is logged in or not.
// if(login_is_success)
// window.location.href = data;
//}
}
else
redirect($this->session->flashdata('prev_page'));
}

I added data-url="true" to the the form in opening <form> tag in question, everything worked fine from there.

Related

Joomla redirect after ajax request fails

An ajax request calls a controller function within my MVC Component.
And in this function there is a Joomla redirect at the end with
JFactory::getApplication()->redirect(JRoute::_($redirect_url, false));
The problem is, that the redirected page won't open! While debugging a session, I can see that the controller will initiate the redirect and the proper details will be sent to view.html.php. But the page, in this case it is a different view in tmpl, will not be open!
Is this because of the ajax call? Do I have to do something in the ajax success part?
Yes, now I do the url preparation in the controller and send back the url to ajax.
$redirect_url = "index.php?option=com_mycomponent&view=my_view&layout=different_layout"
Ajax will do the redirect with the given url.
window.location = result.data;
From there iam struggeling with the SEF part of Joomla to get a SEF friendly URL.

Addressing Codeigniter before and after login

i need to make a login system for my school job, and i want my url on address bar before login and after still the same. Just like facebook, there is url : facebook.com if we're not logged in, and there will be stil facebook.com if we're logged in.
Use HTML5 history API, jQuery,
refer this code examples
example
People use backbone for a one-page app generally.
I assume you are using an authentication library or an authentication system of your own in CodeIgniter.
Inside your default controller you should do something like this (the algorithm)
If user is logged in:
call the layout for the login system
else:
call the main page to be displayed when the user is logged in
For adding other information/pages to it, you could use jquery to replace the body text with the new page.
Where is the new page called from? It can be called from valid CodeIgniter Controllers which provide only the body html.
For example, in your main layout, you could use:
$(document).ready(function(){
$('#button1').click(function(){
$.get("mysite.com/second_page",function(data,status){
$('#main_div').html(data);
});
});
});

CakePHP redirect entire page instead of div

When I'm logging in with my CakePHP login function in the Users controller using ajax. The errors will be displayed in an Div above the login form. But when I have a successful login, I want to redirect the page to the homepage. When I'm using $this->redirect('home'); in the Users controller, the Div will only be updated.
How can I make sure that the entire page will be reloaded? I rather fix this problem with PHP than Javascript 'cause I'm using the Writebuffer method with the JSHelper.
Thanks!
This should be solved in client side. Because client browser gets the AJAX response and puts the output inside the DIV.
Although you can embed javascript code inside the AJAX response. For example, if login is incorrect, then put a window.location javascript code inside the output. But this is not a good solution.
Return an HTTP status code within AJAX response. If for example code is 401, then redirect.
if (data == '401') {
document.location.href="http://example.com";
}

ASP.NET MVC3 - Action without reload site

I have MVC3 app with form for edit data. This for include only dropdownlist.
It is any posibility to execute Controller method (HttpPost) without reload?
If not - how Can I return current site (because I have the same form in different sites).
Yes, try XmlHttpRequest better known as AJAX to execute controllers. It's like sending a request from the browser to the server on a background thread which won't cause a page reload.
have a read of these blogs:
http://www.nikhilk.net/Ajax-MVC.aspx
http://dotnetslackers.com/articles/aspnet/ASP-NET-MVC-2-0-and-AJAX-Part-1.aspx
If you can't use ajax, to get back to the same page you can redirect the user back to the referrer url:
public ActionResult Submit()
{
// do something
return Redirect(Request.UrlReferrer.ToString());
}

browser autocomplete/saved form not work in ajax request

Its really hard to search the any combination of keywords in search engine about this because it used by most popular developer wanted a custom autocomplete by ajax.
Most developer search about the custom autocomplete to get result from db by ajax or about how to disable browser autocomplete due to security reason or they wantted to use another autocomplete extender.
However I am not talking about the autocomplete. I finding about simple normal browser autocomplete or browser saved form either IE or FF that will act like dropdown recent choice during filling a text in a textbox.
It simple and normal filling a form like username and password in a login form. After the form submitted (the form data post) browser will save the autocomplete or event in FF will ask to save along with the password.
Now, think about the login submitted via ajax. The form data not automatically saved by either IE or FF simply because the form not sent by post method. I am pretty sure it is because of ajax vs post method.
CMS like DotNetNuke using this way and its really hard to me to type username and password for 5 user login for development purpose, event I want to let user save their own form data in the browser without any custom or extender. By another example, user can see and use same email to fill an any email form across web site or domain.
How to workaround with this?
Did you have suggestion what keywords is more suitable to search?
CallMeLaNN
I'm having the same problem. I was able to solve it for FireFox by adding a hidden iframe that I submit via JavaScript before doing my AJAX post. I still haven't found anything that works in Chrome/IE.
I've been faced with same issue and searched a bit. I think the solution below is the most convenient way to solve this if you have a login page. If we consider the login submitted via ajax, none of the browsers remember or offer autocomplate feature for user name and password field additionally ask to remember the credentials. But if you use the javascript submit feature (Probably it's not compatible with older versions of browsers), All of the browsers offers to save the username and password except IE. But I've found another javascript tricky for IE to make it offer to save username and password.
In my login page, I've handled the username and password and send them to serverside by ajax request and if the login is succeeded, i submitted the form by the method below otherwise It had been shown an Alert box to the user that the login was failed.
Please Check the link below:
[EDIT]: Link is broken
There is a fixed page about this issue in the page linked, i can not give you another link because of my reputation. Please search for the quotation below in the page:
Look at the fixed page.
Of course,this approach does not fit if you have a login section in the default page because of the form submitting. This causes the page flickering. I wonder if someone has an idea about it?
Here is some unobstrusive js jQuery code that will submit a form both via ajax ($.post method) to a real backend script and also to a dummy script via an iFrame, so the browser will save the submitted data for subsequent autocompletion.
This is working great under chrome. Any feedback is more than welcome!
var formframesindex = 0;
function onSubmitAjax(evt){
var $form = $(this);
var framesubmitting = $form.hasClass('framesubmitting');
var action = $form.attr('action');
var original_action = action;
if(!framesubmitting){
$.post(action,$form.serialize()+"&ajax=1", function(responseText,message,request){
formResponseHandler(responseText);
}, "json");
formframesindex++;
var formframe = $("<iframe name='formframe_id_"+(formframesindex)+"' id='formframe_id_"+(formframesindex)+"' class='formframe' src='/fakeformreceiver.php'></iframe>");
$('body').append(formframe);
var target = $form.attr('target');
$form.data('originaltarget',target);
$form.data('originalaction',original_action);
$form.attr('target','formframe_id_'+formframesindex);
$form.attr('action','/fakeformreceiver.php');
$form.addClass('framesubmitting');
$form.submit();
} else {
var current_target = $form.attr('target');
var original_action = $form.data('originalaction');
var original_target = $form.data('originaltarget');
var $frame = $('#'+current_target);
setTimeout(function(){
if($frame && $frame.length){
$frame.remove();
}
$form.attr('action',original_action);
$form.attr('target',original_target);
$form.removeClass('framesubmitting');
},100);
}
return framesubmitting;
}

Resources