yeyuan 发表于 2013-1-28 09:33:04

JQuery ajax 登陆

<form action="test.php" name="login"method="post">用户名:<input type="text" name="username" id="username" />密码:<input type="text" name="passwork" id="passwork" /><input type="button" value="登录" id="login" /></form><div id="result"></div>

$(function (){    $('#login').click(function (){         var dataString ='username='+$('#username').val()+'&password='+$('#password').val();          $.ajax({             url:'test.php',             type:'post',             data:dataString,             success:function(data){                if(data == '1'){                  $('#result').html('登陆成功');                }                else if(data == '0'){                  $('#result').html('登录失败');                }                else{                   $('#result').html(data);                }             }         });    });});

<?php mysql_connect('localhost','root','');mysql_select_db('news');mysql_query('set names utf8');$u = $_POST['username'];$p = $_POST['passwork'];if($u == '' || $p == ''){    echo "不能为空";    exit;}$sql = "select * from tuser ";$result = mysql_query($sql);$num = mysql_num_rows($result);/*$sql ="insert into tuser value(null,'$u','$p')";$result = mysql_query($sql);$num = mysql_insert_id();//插入数据*/if($num >0){   echo "1";}else{   echo '0';}?>

从上面可以看出来可以把验证写在后台的php文件里进行验证,也可以在前台通过js进行验证。
页: [1]
查看完整版本: JQuery ajax 登陆