如何保留从PHP获取后显示在HTML中的值

How to retain the values displayed in the HTML after it is fetch from the PHP?

本文关键字:显示 HTML 获取 PHP 何保留 保留      更新时间:2023-09-26

我有一个HTML页面,它接受用户输入并根据数据库显示输出。我有一个到其他页面的超链接。我想当我从第一页导航到其他HTML页面,我添加一个后退按钮,它应该返回到第一页,但它应该显示获取的值。下面是代码:

HTML 1:

<script>
function PostData() {
var online = navigator.onLine;
if(online){
    // 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', 'login3.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
}
else{
alert("You are offline");
}
}
</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>
</body>
PHP:

<?php
if(isset($_POST['userid'],$_POST['pid']))
{
    $userid = trim($_POST["userid"]);
    $pid = trim($_POST["pid"]);
    $sql = "SELECT * FROM demo WHERE username = '$userid' and password = '$pid'";
    $result = mysqli_query($conn,$sql);
    $row = mysqli_fetch_array($result);
    echo $row['week'].'<br/>'.'<br/>';
    echo '<a href="2ndHTML.html"/>'.$row['day1'].'</a>'.'<br/>';
?>

HTML 2:

<body>
  <form enctype="multipart/form-data" id="form" action="" method="post">
        <input type="file"  id="imageid" name="image" onchange="readURL();" />
        <img id="blah" src="#" alt="your image" /><br/><br/>
        <input type="button" value="upload" onclick="javascript:uploadInage();" />
        <a href = "1stHTML.html">BACK</a>
    </form>
</body>

我想保留从1sthhtml .html中获取的值

最好使用session。一旦用户完成了第一个表单,设置一个会话来发出信号,这样当他们返回到第一个页面时,它会读取会话并自动将他们重定向到必要的页面。

你需要把这个放在你的1sthtml.php2ndhtml.php页面的顶部,以表明你想使用会话:

<?php
session_start();

1sthtml.php页面上,您需要设置会话信息:

<?php
if(isset($_POST['userid'],$_POST['pid']))
{
    $userid = trim($_POST["userid"]);
    $pid = trim($_POST["pid"]);
    $sql = "SELECT * FROM demo WHERE username = '$userid' and password = '$pid'";
    $result = mysqli_query($conn,$sql);
    $row = mysqli_fetch_array($result);
    echo $row['week'].'<br/>'.'<br/>';
    echo '<a href="2ndHTML.html"/>'.$row['day1'].'</a>'.'<br/>';
    // ---- SET SESSION HERE ---
    $_SESSION['stage'] = 1;
}
?>

然后,在1sthtml.php上,您需要再次检查会话变量是否存在,如果存在,则转发到您想要的页面。因此,在1sthtml.php的顶部,在之前的session_start()旁边:

<?php
session_start();
if (isset($_SESSION['stage'])) {
    header('Location: 2ndhtml.php');
    exit();
}