将具有POST的PHP变量发送到另一个PHP文件

时间:2019-05-08 11:24:43

标签: javascript php html mysql ajax

我正在做一个页面,该页面通过AJAX将用户的HTML输入发送到PHP,但是我需要该信息才能通过几次验证后打开另一个PHP页面

我试图将文件包含在另一个文件中并使用该变量,但是我得到了“未定义索引用户”,这是因为我没有在新文件中对其进行定义。

HTML

<div class="wrap-input100 validate-input" data-validate = "Enter username"><input class="input100" type="text" id="user" name="username" placeholder="username">
<span class="focus-input100" data-placeholder="&#xf207;"></span>
</div>

<div class="wrap-input100 validate-input" data-validate="Enter password">
<input class="input100" type="password" id="pass" name="pass" placeholder="password">
<span class="focus-input100" data-placeholder="&#xf191;"></span>
</div>

<div class="container-login100-form-btn">
<a class="login100-form-btn" id = "logBtn">
Login
</a>
</div>      

JS

$('#logBtn').click(function(event){ 
    user = document.getElementById("user").value;
    password = document.getElementById("pass").value;

    $.ajax({
        type:"POST",
        url:"login.php",
        async: false,
        data: {user:user,password:password},
        success: function(data){
            if(data == 'Administrator'){
                window.location.href='../Main/index.php';
             }
             else if(data == 'Emp'){
                window.location.href='../Main/startemp.html';
             }else{
               alert("username or password invalid");
             }
        }
        });
    });

login.php

<?php
$servername = "localhost";
$username = "root";
$password = "asdadadas";
$dbname = "dsdvvt";

$conn = new mysqli($servername, $username, $password, $dbname);

$user = $_POST['user'];
$pass = $_POST['password'];

$sql = "SELECT * FROM users WHERE email='$user' AND clave='$pass'"; 
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    $sql_1 = "SELECT * FROM users WHERE email='$user' AND clave='$pass' AND permisos='Administrador'";
    $result_1 = mysqli_query($conn, $sql_1);
    if (mysqli_num_rows($result_1) > 0){
        echo "Administrator";
        exit(0);
    }
    else{
        echo "Emp";
        exit(0);
    }

 } else {
    $msg = "Username or password invalid";
    echo $msg;
 }

mysqli_close($conn);
?>

index.php

<?php
include '../Login/login.php';
$one = $user;?>

我需要我在login.php中收集的信息才能将其发送到index.php,有什么方法可以做到这一点?

1 个答案:

答案 0 :(得分:0)

我的建议是参加会议。

与...开始会话

<?php
session_start();
?>

并分配这样的变量:

$_SESSION['user'] = "FlowMafia";

打印这样的会话变量。

echo $_SESSION['user'];

有关更多信息,请阅读本文: https://www.php.net/manual/en/intro.session.php