weixin_33721427 2014-11-27 02:42 采纳率: 0%
浏览 23

AJAX> PHP登录无法正常工作

I'm trying to create a log in form using html > ajax > php, the problem is the php is not working, I don't know where is the problem, I think the ajax cannot execute my php file. I need help. Thanks in advance.

Here is my HTML code: my form and inputs are below

          <form id="loginForm">
            <input type="text" data-clear-btn="true" name="username" id="username" value="" placeholder="Username / ID No.">
            <input type="password" data-clear-btn="true" name="password" id="password" value="" placeholder="Password">
            <input type="checkbox" name="rem_user" id="rem_user" data-mini="true">
            <label for="rem_user">Remember me</label>
            <input type="submit" name="login" id="login" value="Log in" class="ui-btn" />
          </form>
          <div class="err" id="add_err"></div>

AJAX script that sends request on my php file

    <script>
        $(document).ready(function(){
            $("#loginForm").submit(function(){
                var username = $("#username").val();
                var password = $("#password").val();
                // Returns successful data submission message when the entered information is in database.
                var dataString = 'username=' + username + '&password=' + password;
                    if (username == '' || password == ''){
                        alert("Please Fill All Fields");
                    }
                    else {
                    // AJAX Code To Submit Form.
                        $.ajax({
                            type: "POST",
                            url: "php/login-action.php",
                            data: dataString,
                            success: function(result){
                                window.location="#report_page";
                            }
                        });
                    }
                return false;
            });
        });
    </script>

PHP File

<?php

require "includes/connection.php";
include "includes/function.php";
if(isset($_POST['login'])){


    $username = $_POST['username'];
    $password = $_POST['password'];

    $username = sanitize($username);
    $password = sanitize($password);
    $pass2 = md5($password);
    $salt = "sometext";
    $validateHash = $salt.$pass2;
    $pass = hash("sha512", $validateHash);

    $sql = "SELECT * FROM user_login WHERE username='".$username."' and password='".$password."'";
    $result = mysqli_query($con,$sql) or die("Error: ". mysqli_error($con));

    $count=mysqli_num_rows($result);

    while($row=mysqli_fetch_array($result))
        {
            $id = $row['user_id'];
            $username = $row['username'];
            $name = "".$row['firstname']." ".$row['lastname']."";
            $acc_type = $row['Acc_Type'];
        }
        if($count==1){
            if($acc_type == 'user') {
                $_SESSION["id"] = $id;
                $_SESSION["username"] = $username;
                $_SESSION["name"] = $name;
                echo 'true';
            }
            else {
                echo 'false';
            }

        }


}
?>
  • 写回答

2条回答 默认 最新

  • 撒拉嘿哟木头 2014-11-27 02:53
    关注

    as Cattla mentioned in comments.

    Your PHP is looking for $_POST['login'], and your $.ajax call didn't pass that in.

    so here is the answer

    var dataString = 'login=login&username=' + username + '&password=' + password;
    

    Debug tips

    • Did ajax send all required inputs to PHP (you can inspect this from browser developer tool)
    • Did php receive all required inputs (you could var_dump($_POST)
    • Did php connect to mysql successfully
    • Did ajax receive data from PHP (use alert or console.log)
    评论

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)