duanjiangzhi6851 2015-06-05 04:00
浏览 52

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

I have an HTML page that takes the user input and displays the output based on the database. I have a hyperlink to the other pages. I want when I navigate from first page to other HTML page, I add a back button and it shoud return to the first page but it should show the fetched values. Here is the code below.

1st HTML:

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

2nd HTML:

<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>

I want to retain the values fetched on the 1stHTML.html

  • 写回答

1条回答 默认 最新

  • dqmchw0071 2015-06-05 07:14
    关注

    It's best to use session. Once the user has completed the first form set a session to signal that, so when they return to the first page it will read the session and automatically redirect them to the necessary page.

    You'll need to put this at the top of your 1sthtml.php and 2ndhtml.php page to signal that you want to use sessions:

    <?php
    session_start();
    

    On your 1sthtml.php page you'll need to set the session information:

    <?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;
    }
    ?>
    

    And then, on the 1sthtml.php again you'll need to check to see if that session variable exists, if it does then forward onto the page you want. So, at the top of your 1sthtml.php, next to your previous session_start():

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

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大