Ashvin
Ashvin

Reputation: 4020

How to call jquery function with php script...?

I tried to make a login module. I require the ability to show the message "UNAME OR PASS INCORRECT", but when uname and pass are not matching I am not able to display that error message div.

So does anyone have an idea about how to display that div when user is not authenticated?

Thanks in advance.

Upvotes: 0

Views: 595

Answers (4)

gion_13
gion_13

Reputation: 41533

Like GOV said, you probably want to do this via ajax calls because it doesn't require the user to leave the page as he tries to login.

If you use jquery, the script would be very simple, such as :

$.post(url,{user:UNAME,pass:PASS},function(data){ 
   try{
    // EDIT: server MUST return response in text/plain as "true" or "false".
    // EDIT: if(data) changed to if(data == "true")
    if(data == "true")   
      window.location = redirectPage;
    else
      showError();
   }catch(e){
      showError();
   } 
});
function showError(){
   $('#'+errorDivID).show();
}

where url is the url of the page that verifies the user & pass combination, which returns "true" if the user & pass values are true and "false" otherwise.

UNAME and PASS are variables that hold the user info, redirectPage is the url of the page you want to redirect your authentificated users, and errorDivId is the id of the div element that you want to show. The error DIV probably contains a warning message such as "invalid user+pass combination !".

Hope this helps!

EDIT: Edited to remove "eval" as it's not necessary if the server is returning a boolean.

Upvotes: 1

skoTner
skoTner

Reputation: 168

I would presume you could do a jQuery POST to a PHP script that handels your login.

<form id='login'>
  <input type='text' name='username' id='username'>
  <input type='password' name='password' id='password'>
</form>

Then posting this form with jQuery instead of PHP

$.post("login.php", { username: username, password: password } );

This would post the username and password field to login.php. Then have login.php output something you can process in the same jQuery script. Have PHP return true if login is correct and false if not, then have jQuery display an error if the PHP script returns false and have jQuery do whatever as correct login if the PHP returns true.

Upvotes: 0

Sander
Sander

Reputation: 1274

You indeed don't have to specifically use PHP for this. A good way to do this is the separation of model (your database with the user information and the queries used to access and retrieve data from it), your controller which is putting everything in motion and sending data to your template and your view (the template).

The above structure makes also clear that PHP is not specifically used for this, PHP should only have the role to access your database and give data back to the template.

Have a look at jQuery or Prototype with the Ajax examples, once you get the hang of that a whole new world opens up. :)

Upvotes: 0

kobe
kobe

Reputation: 15835

Ashvin ,

You need to use ajax for this

http://api.jquery.com/jQuery.ajax/

make ajax call to the database and let php send a resposne back to the UI either success or failure.

If it is failure , lets show a div with different colors...

Upvotes: 0

Related Questions