缓存信息并保留,直到刷新页面

时间:2015-05-27 03:53:40

标签: javascript php jquery html

我有一个html页面可以从用户获取登录用户名和密码,并使用PHP从数据库中获取信息。我使用javascript将变量用户名和密码传递给PHP,因为我不想显示后台进程。以下是代码:

对于HTML

<script>
function PostData() {
// 1. Create XHR instance - Start
var xhr;
if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        xhr = new ActiveXObject("Msxml2.XMLHTTP");
    }
    else {
        throw new Error("Ajax is not supported by this browser");
    }
    // 1. Create XHR instance - End
    // 2. Define what to do when XHR feed you the response from the server - Start
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status == 200 && xhr.status < 300) {
                document.getElementById('div1').innerHTML = xhr.responseText;
            }
        }
    }
    // 2. Define what to do when XHR feed you the response from the server - Start

    var userid = document.getElementById("userid").value;
    var pid = document.getElementById("pid").value;
    // var image = document.getElementById("image").value;
    // 3. Specify your action, location and Send to the server - Start 


    xhr.open('POST', 'login.php');
    //xhr.open('POST', 'config.php');
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send("userid=" + userid + "&pid=" + pid);
    //xhr.send("&pid=" + pid);
    // 3. Specify your action, location and Send to the server - End

}

</script>
</head>
<body>
<form>
    <label for="userid">User ID :</label><br/>
    <input type="text" name ="userid" id="userid"  /><br/>
    <label for="pid">Password :</label><br/>
    <input type="password" name="password" id="pid" /><br><br/> 
    <div id="div1">
    <input type="button" value ="Login" onClick="PostData()" />
    </div>
</form>

对于PHP

<?php 
ini_set('display_errors', 1);
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dtable";

//session_start();
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$upload = "E:/Diczon";

$str = '';
$str1 = '';
if(isset($_POST['userid'],$_POST['pid']))
{
  $userid = trim($_POST["userid"]);
  $pid = trim($_POST["pid"]);

  $sql = "SELECT * FROM template WHERE uname = '$userid' and pword = '$pid'";
  $result = mysqli_query($conn,$sql);
  $row = mysqli_fetch_array($result);

  echo "公司".'<br/>';
  echo $row['client'].'<br/>'.'<br/>';
  echo "第".'<br/>';
  echo '<a href="preview.html"/>'.$row['day1'].'</a>'.'<br/>';
?>

此代码在同一HTML页面上显示输出。

现在您可以看到我有一个指向另一个HTML页面的超链接。在这个HTML页面中,我正在上传图片。所以上传完成后我想添加一个BACK按钮并将其链接到登录过程完成后显示输出的页面。但是如果我将BACK按钮超链接到第一个HTML页面,它将再次提示登录。所以请告诉我如何将数据保存在第一页并使用BACK按钮返回到它。这是其他HTML页面的代码。

第二个HTML

<body>
<form enctype="multipart/form-data" id="form" action="login.php" method="POST">
  <input type='file' onchange="readURL(this);" name="image" id="image" /><br/><br/>
  <img id="blah" src="#" alt="your image"  /><br/><br/>
  <input type='file' onchange="readURL(this);" name="image1"  /><br/><br/>

    <input type="submit" value="Upload"/>
    </form>
    <a href="index.html">BACK</a>//Want to retrieve the output data using this BACK button.
</body>

1 个答案:

答案 0 :(得分:0)

您必须保持用户登录。您可以使用会话和/或cookie。

您需要的PHP功能:

<?php
    //This function starts a session and must be called on your form HTML page, before your submit
    session_start();
?>

然而,您应该能够将数据从数据库保存到全局$ _SESSION,如下所示:

<?php 
    //Avoid using * for queries...
    $sql = "SELECT * FROM template WHERE uname = '$userid' and pword = '$pid'";
    $result = mysqli_query($conn,$sql);

    $row = mysqli_fetch_array($result);

    //It's missing some validations here, you should consider it...

    //The magic is here, you're saving your client in $_SESSION
    $_SESSION['client'] = $row['client'];
?>

保存在$ _SESSION后,您就可以在新页面上找到您的客户:

<?php
    print_r($_SESSION);

    echo $_SESSION['client'];
?>
相关问题