Home > database >  Redirect user to login page from JQuery autocomplete if not logged in
Redirect user to login page from JQuery autocomplete if not logged in

Time:11-02

  1. When users log into my PHP website, they are automatically logged out after a period of inactivity.
  2. I have a few input fields on the main page for searching for stuff. Input type="text" with JQuery autocomplete option on them. The JQuery autocomplete takes user input and sends it to a PHP file that searches the database and returns data in JSON format.
  3. The original page displays the data returned in a clickable list.

Now, if users have been logged out due to inactivity, this isn't immediately obvious, unless of course they choose to reload the page. Most don't, however. So if they just start entering search words, they just get nothting, because they're not logged in.

I already have a routine to redirect users to the login page if they're not logged in. But this doesn't work in this case, because it's the JSON-generating page that is being redirected, that that doesn't really have any influence on the main page.

Also, I can't see how I can interact directly with what's being returned - it seems like JQuery does all of the data handling internally, so I can't really grab onto data returned and tell the main page to redirect in that case.

How do I achieve the desired result?

CodePudding user response:

On server side ajax first check login condition and based on that give respons e like {login:false} something like that, in your jquery function check that response and if login=false then redirect using window.location.

  • Related