dougu4704 2013-05-12 01:35
浏览 37

Ajax和php - 登录脚本

I'm trying to do a login page, but I have an issue wit ajax method:

 $("#login").click(function(){

        username=$("#user_name").val();
        password=$("#password").val();
         $.ajax({
            type: "POST",
            url: "login.php",
            data: {name : username, pwd : password},
            success: function(html){
              if( ($.trim(html)) =='true')
              {
                $("#login_form").fadeOut("normal");
                        $("#shadow").fadeOut();
                        $("#profile").html("<a href='logout.php' id='logout'>Logout</a>");

              }
              else
              {
                    $("#add_err").html("Wrong username or password");
              }
            },
            beforeSend:function()
                {
                 $("#add_err").html("Loading...")
            }
        });
         return false;
    });

login.php:

<?php  

 session_start();

 $username = $_POST['name'];
 //$username = $mysqli->real_escape_string($username);
 $password = ($_POST['pwd']);
 //$password = $mysqli->real_escape_string($password);

 $mysqli = mysqli_connect('localhost','root','','peer');
 $query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
 $result = mysqli_query($mysqli,$query)or die(mysqli_error());
 $num_row = mysqli_num_rows($result);
 $row=mysqli_fetch_array($result);
 if( $num_row >=1 ) {
  echo 'true';
  $_SESSION['user_name']=$row['username'];
 }
 else{
 echo 'false';
 }
?>

I'm posting to login.php username and password, but obviously get nothing back, when I tried execute only php script it works correct. A html object seems like is still false. Do you see any mistake here ??

thanks

EDIT!

  • 写回答

2条回答 默认 最新

  • doujia2386 2013-05-12 01:50
    关注

    These are the issues I see here

    • your data mat not be properly encoded. Instead of data: "name="+username+"&pwd="+password, use data:{name:username,pwd:password} so that jQuery will properly encode it for you.
    • you response may have leading or trailing white spaces, so try if($.trim(html)=='true')
    • always validate/sanitize input, you should use parameterized prepared statements, or at least escape the fields.
    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数