jquery ajax登陆代码,无刷新提示错误
jquery ajax登陆代码,无刷新提示错误
测试页面用了jquery.validate.min.js判断email等是否合法,可以在不同情况下看是否需要。
js代码
JavaScript Code复制内容到剪贴板
- <script>
- $(document).ready(function(){
- $('#login').validate({
- rules: {
- usrpass: {
- required: true
- },
- usremail: {
- required: true,
- email: true
- }
- },
- messages: {
- usrpass: {
- required: 'Password is required.'
- },
- usremail: {
- required: 'Email is required.',
- email : 'Invalid Email.'
- }
- }
- }); // end register validation
- });
- </script>
主要代码
XML/HTML Code复制内容到剪贴板
- <div id="DivLogin">
- <form action="" id="login" method="POST">
- <table>
- <tr>
- <td>Email :</td>
- <td><input type="text" id="usremail" name="usremail" /></td>
- </tr>
- <tr>
- <td>Password :</td>
- <td><input type="password" id="usrpass" name="usrpass" /></td>
- </tr>
- <tr>
- <td><input type="hidden" name="login" value="login" /></td>
- <td><input type="submit" id="submit" name="submit" value="Submit" /></td>
- </tr>
- <tr>
- <td colspan="2"><?php echo (isset($msg) ? '<font color="red">'.$msg.'</font>': '');?></td>
- </tr>
- </table>
- </form>
- </div>
php代码
PHP Code复制内容到剪贴板
- <?php
- session_start();
- if (isset($_POST['login']) and $_POST['login'] == 'login')
- {
- include 'conn.php';
- $email = mysql_real_escape_string($_POST['usremail']);
- $password = mysql_real_escape_string($_POST['usrpass']);
- $password = md5($password);
- $sql = "select count(*) from username_list where email='$email' and password='$password'";
- $result = mysql_query($sql);
- if (!$result)
- {
- echo 'Error Saving Data. ';
- exit();
- }
- $row = mysql_fetch_array($result);
- if ($row[0] > 0)
- {
- $_SESSION['loggedIn'] = true;
- header ('Location: welcome.php');
- exit();
- }
- else
- {
- $msg = 'Wrong email or password.';
- }
- }
- ?>
以上代码放在登陆页就可以了
原文地址:http://www.freejs.net/article_biaodan_18.html