Php ajax allows users to log on and quickly implement user authentication. For more information, see.
Core code
| The Code is as follows: |
Copy code |
|
<Script type = "text/javascript">
$ (Document). ready (function (){
$ ("# Login"). click (function (){
Var action = $ ("# form1"). attr ('action ');
Var form_data = {
Username: $ ("# username"). val (),
Password: $ ("# password"). val (),
Is_ajax: 1
};
$. Ajax ({
Type: "POST ",
Url: action,
Data: form_data,
Success: function (response)
{
If (response = 'success ')
$ ("# Form1"). slideUp ('low', function (){
$ ("# Message" Login .html ("<p class = 'success'> login successful! </P> ");
});
Else
$ ("# Message" cmd.html ("<p class = 'error'> incorrect username or password. </p> ");
}
});
Return false;
});
});
</Script> |
Index. php source code
| The Code is as follows: |
Copy code |
|
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> PHP Login with jQuery AJAX </title>
<Style>
Body, input {
Font-family: Georgia, "Times New Roman", Times, serif;
}
# Content {
Background-color: # eee;
Width: 300px;
Margin: 0 auto;
Border: 1px solid # aaa;
-Moz-border-radius: 10px;
-Moz-box-shadow: 0 0 10px # aaa;
-Webkit-border-radius: 10px;
-Webkit-box-shadow: 0 0 10px # aaa;
Padding: 10px;
}
# Form1 label {
Display: block;
Font-size: 16px;
Line-height: 25px;
}
# Form1 input [type = text], # form1 input [type = password] {
Padding: 2px;
Font-size: 16px;
Line-height: 20px;
Width: 250px;
}
# Form1 input [type = submit] {
Font-size: 20px;
Font-weight: bold;
Padding: 5px;
}
. Success {
Color: #060;
}
. Error {
Color: red;
}
</Style>
<Script type = "text/javascript" src = "jquery-1.5.2.min.js"> </script>
<Script type = "text/javascript">
$ (Document). ready (function (){
$ ("# Login"). click (function (){
Var action = $ ("# form1"). attr ('action ');
Var form_data = {
Username: $ ("# username"). val (),
Password: $ ("# password"). val (),
Is_ajax: 1
};
$. Ajax ({
Type: "POST ",
Url: action,
Data: form_data,
Success: function (response)
{
If (response = 'success ')
$ ("# Form1"). slideUp ('low', function (){
$ ("# Message" cmd.html ("<p class = 'success'> You have logged in successfully! </P> ");
});
Else
$ ("# Message" cmd.html ("<p class = 'error'> Invalid username and/or password. </p> ");
}
});
Return false;
});
});
</Script>
</Head>
<Body>
<P> & nbsp; </p>
<Div id = "content">
<H1> Login Form
<Form id = "form1" name = "form1" action = "doLogin. php" method = "post">
<P>
<Label for = "username"> Username: </label>
<Input type = "text" name = "username" id = "username"/>
</P>
<P>
<Label for = "password"> Password: </label>
<Input type = "password" name = "password" id = "password"/>
</P>
<P>
<Input type = "submit" id = "login" name = "login"/>
</P>
</Form>
<Div id = "message"> </div>
</Div>
</Body>
</Html> |
Dogloin. php
| The Code is as follows: |
Copy code |
|
<? Php
$ Is_ajax =$ _ REQUEST ['is _ ajax '];
If (isset ($ is_ajax) & $ is_ajax)
{
$ Username = $ _ REQUEST ['username'];
$ Password = $ _ REQUEST ['Password'];
If ($ username = 'Demo' & $ password = 'Demo ')
{
Echo "success ";
}
}
?> |