Javascript 缓存信息并将其保留到页面刷新

Javascript 缓存信息并将其保留到页面刷新,javascript,php,jquery,html,Javascript,Php,Jquery,Html,我有一个html页面,可以从用户那里获取登录用户名和密码,并使用PHP从数据库中获取信息。我正在使用javascript将变量username和password传递给PHP,因为我不想显示后台进程。代码如下: 用于HTML <script> function PostData() { // 1. Create XHR instance - Start var xhr; if (window.XMLHttpRequest) { xhr = new XMLHttpRequ

我有一个html页面,可以从用户那里获取登录用户名和密码,并使用PHP从数据库中获取信息。我正在使用javascript将变量username和password传递给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>

函数PostData(){
//1.创建XHR实例-启动
var-xhr;
if(window.XMLHttpRequest){
xhr=newXMLHttpRequest();
}
else if(window.ActiveXObject){
xhr=新的ActiveXObject(“Msxml2.XMLHTTP”);
}
否则{
抛出新错误(“此浏览器不支持Ajax”);
}
//1.创建XHR实例-结束
//2.定义当XHR从服务器向您提供响应时要做什么-启动
xhr.onreadystatechange=函数(){
if(xhr.readyState==4){
如果(xhr.status==200&&xhr.status<300){
document.getElementById('div1')。innerHTML=xhr.responseText;
}
}
}
//2.定义当XHR从服务器向您提供响应时要做什么-启动
var userid=document.getElementById(“userid”).value;
var pid=document.getElementById(“pid”).value;
//var image=document.getElementById(“image”).value;
//3.指定您的操作、位置并发送到服务器-启动
open('POST','login.php');
//open('POST','config.php');
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
xhr.send(“userid=“+userid+”&pid=“+pid”);
//xhr.send(“&pid=“+pid”);
//3.指定您的操作、位置并发送到服务器端
}
用户ID:

密码:


对于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页面中,我上传了一个图像。因此,上传完成后,我想添加一个后退按钮,并将其链接到登录过程完成后显示输出的页面。但是如果我将后退按钮超链接到第一个HTML页面,它将提示再次登录。因此,请让我知道如何保持第一页上的数据,并返回到它使用返回按钮。下面是另一个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>







//要使用此后退按钮检索输出数据。
您必须让用户保持登录状态。您可以使用会话和/或cookie

您将需要的PHP函数:

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

然后,您应该能够将数据从数据库保存到全局$\会话,如下所示:

<?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'];
?>

在$u会话中保存后,您将能够在新页面上实现您的客户端:

<?php
    print_r($_SESSION);

    echo $_SESSION['client'];
?>


切勿在SQL中对用户定义的值使用字符串链接。使用参数。非常感谢。我会考虑。也考虑加密密码。如果我想在HTML页面上显示这个输出,该怎么办?例如,PHP进行身份验证并获取结果。但是我想在HTML页面中显示输出,因为我想在manifest.appcache中添加该页面,这样即使没有网络连接,用户也可以导航到不同的页面。您可以使用或。好的想法是cookies PHP与cookies JS对话,因此您可以轻松地将一个传输到另一个。