Javascript 页面重新加载时如何取回设置的数据

Javascript 页面重新加载时如何取回设置的数据,javascript,php,jquery,Javascript,Php,Jquery,我正在为我工作的BPO编写一个时间管理系统,同时编写这种“撞墙”。我试着搜索,但没有找到对我有用的东西,但根据阅读的内容,我试着做了一些最终不起作用的事情 我的问题是,我使用一个日期选择器和一个员工下拉列表来搜索他们用另一个表单输入到数据库中的时间细节。我使用JavaScript发送搜索变量并创建PHP表,如果搜索成功,该表将显示在DIV中 以下是我的初始代码: 主搜索页 <?php session_start(); include_once ("../Functions/userChec

我正在为我工作的BPO编写一个时间管理系统,同时编写这种“撞墙”。我试着搜索,但没有找到对我有用的东西,但根据阅读的内容,我试着做了一些最终不起作用的事情

我的问题是,我使用一个日期选择器和一个员工下拉列表来搜索他们用另一个表单输入到数据库中的时间细节。我使用JavaScript发送搜索变量并创建PHP表,如果搜索成功,该表将显示在
DIV

以下是我的初始代码:

主搜索页

<?php
session_start();
include_once ("../Functions/userCheck.php");
?>

<html>
<head>

    <!-- Style Sheets -->
    <link rel="stylesheet" type="text/css" href="../../CSS/main.css">
    <link rel="stylesheet" type="text/css" href="../../CSS/jquery-ui.min.css">

    <!-- Java Scripts -->
    <script language="JavaScript" type="text/javascript" src="../../jScripts/jquery-3.2.0.min.js"></script>
    <script language="JavaScript" type="text/javascript" src="../../jScripts/jquery-ui.min.js"></script>
    <script language="JavaScript" type="text/javascript" src="../../jScripts/editTimeEntryFunctions.js"></script>
    <script language="JavaScript" type="text/javascript" src="../../jScripts/timeEditResultFunctions.js"></script>
    <script language="JavaScript" type="text/javascript" src="../../jScripts/getUser.js"></script>
    <script language="JavaScript" type="text/javascript" src="../../jScripts/getIds.js"></script>
    <script language="javascript" type="text/javascript" src="../../jScripts/getCat.js"></script>
    <script language="javascript" type="text/javascript" src="../../jScripts/getClient.js"></script>
    <script language="JavaScript" type="text/javascript" src="../../jScripts/getIds.js"></script>
    <script language="JavaScript" type="text/javascript" src="../../jScripts/reload.js"></script>
    <script language="JavaScript" type="text/javascript" src="../../jScripts/setMsg.js"></script>
    <script language="JavaScript" type="text/javascript" src="../../jScripts/sendData.js"></script>
</head>
<body onload="getUser(); checkGetData();">
<div id="divCenter-timeEdit" class="box">
    <div class="logo-timeEdit">
        <img src="../../images/logo.png" width="142" height="33">
    </div>
    <div id="mainDiv" style="height: 38px;">
        <label for="dPicker">Date:</label>
        <input type="text" id="dPicker" style="margin-left: .5%;" size="10">
        <label for="userSelect" style="margin-left: 2%">Select User:</label>
        <select id="userSelect" style="width:160px; margin-left: .5%;" onchange="usrId(this.id);"></select>
        <input type="text" id="uid" size="1" hidden>
        <input type="button" class="getData" value="Submit" onclick="getData();">
    </div>
    <div id="resultTable"><span id="noDataMsg"></span> </div>
</div>
</body>
</html>
我的函数PHP

<?php
session_start();
include_once("../iConnect/handShake.php");
include_once ("../Functions/userCheck.php");

if ($_REQUEST["date"] != "" && $_REQUEST["uid"] != ""){

//  Set data session to be used when page reloads to keep show the data intact in the table
    $_SESSION["date"] = $_REQUEST["date"];
    $_SESSION["uid"] = $_REQUEST["uid"];

//  Check if the there's any data in the database if there is it will be passed on to the query
    $getCount = "SELECT COUNT(*) FROM usertimetrack WHERE jDate = :jDate AND usrId = :usrId";
    $getCountQuery = $dbConnect -> prepare($getCount);
    $getCountQuery -> bindParam(':jDate', $_REQUEST["date"]);
    $getCountQuery -> bindParam(':usrId', $_REQUEST["uid"]);
    $getCountQuery -> execute();
    $rowCount = $getCountQuery -> fetchColumn();

    if ($rowCount > 0){
//      This will select all the details in the database according to the entered data and the user who entered it
//      Which will be displayed as a table in the main PHP file.
        $getJobs = "SELECT usertimetrack.*, userlogin.uName, catdb.Catagory, clientdb.Client FROM usertimetrack
                    INNER JOIN userlogin ON usertimetrack.usrId = userlogin.uId
                    INNER JOIN catdb ON usertimetrack.Category = catdb.catId 
                    INNER JOIN clientdb ON usertimetrack.utClient = clientdb.clientId
                    WHERE jDate = :jDate AND usrId = :usrId";
        $getJobsQuery = $dbConnect -> prepare($getJobs);
        $getJobsQuery -> bindParam(':jDate', $_REQUEST["date"]);
        $getJobsQuery -> bindParam(':usrId', $_REQUEST["uid"]);
        $getJobsQuery -> execute();
?>
<html>
<head>

<!--    <!-- Style Sheets -->
<!--    <link rel="stylesheet" type="text/css" href="../../CSS/main.css">-->
<!--    <link rel="stylesheet" type="text/css" href="../../CSS/jquery-ui.min.css">-->
<!---->
<!--    <!-- Java Scripts -->
<!--    <script language="JavaScript" type="text/javascript" src="../../jScripts/jquery-3.2.0.min.js"></script>-->
<!--    <script language="JavaScript" type="text/javascript" src="../../jScripts/jquery-ui.min.js"></script>-->

</head>
<body>
    <div id="mainDiv">
        <div id="navi"></div>
        <div id="infoi"></div>
        <table id="hor-minimalist-b" style="table-layout: fixed; width: 96%; margin-left: 2%;">
            <div id="bgDimmer"></div>
            <div id="divContent" style="width: 65%; margin-left: 20%; margin-top: -8%;"></div>
            <thead>
            <tr>
                <th scope="col" style="width: 6%;">User Name</th>
                <th scope="col" style="width: 10%;">Category</th>
                <th scope="col" style="width: 10%;">Client</th>
                <th scope="col" style="width: 6%;">Start Time</th>
                <th scope="col" style="width: 6%;">End Time</th>
                <th scope="col" style="width: 6%;">Time Spent</th>
                <th scope="col" style="width: 6%;">Volume</th>
                <th scope="col" style="width: 6%;">Prod. Lines</th>
                <th scope="col" style="width: 16%;">Remarks</th>
                <th scope="col" style="width: 10%;"></th>
            </tr>
            </thead>
            <tbody>
            <?php while ($getJobsRow =$getJobsRow = $getJobsQuery -> fetch(PDO::FETCH_ASSOC)) { ?>
                <tr>
                    <td><?php echo $getJobsRow["uName"]; ?></td>
                    <td><?php echo $getJobsRow["Catagory"]; ?></td>
                    <td style="word-wrap:break-word;"><?php echo $getJobsRow["Client"]; ?></td>
                    <td><?php echo $getJobsRow["startTime"]; ?></td>
                    <td><?php echo $getJobsRow["endTime"]; ?></td>
                    <td><?php echo $getJobsRow["timeSpent"]; ?></td>
                    <td><?php echo $getJobsRow["Volume"]; ?></td>
                    <td><?php echo $getJobsRow["noOfProductLines"]; ?></td>
                    <td style="word-wrap:break-word; text-align: left"><?php echo $getJobsRow["Remarks"]; ?></td>
                    <td>
                        <input type="button" class="utEdit" value="Edit" data-utid="<?php echo $getJobsRow["utId"]; ?>" onmousedown="popup();" />
                        <input type="button" class="utDel" value="Delete" data-utid="<?php echo $getJobsRow["utId"]; ?>" onmousedown="popup();" /></a>
                    </td>
                </tr>
            <?php }?>
            </tbody>
        </table>
        <?php }else{ ?>
            <span id="noDataMsg">There's no data to display</span>
        <?php }}elseif($_SESSION["date"] != "" && $_SESSION["uid"] != ""){

        //  Check if the there's any data in the database if there is it will be passed on to the query
        $getCount = "SELECT COUNT(*) FROM usertimetrack WHERE jDate = :jDate AND usrId = :usrId";
        $getCountQuery = $dbConnect -> prepare($getCount);
        $getCountQuery -> bindParam(':jDate', $_SESSION["date"]);
        $getCountQuery -> bindParam(':usrId', $_SESSION["uid"]);
        $getCountQuery -> execute();
        $rowCount = $getCountQuery -> fetchColumn();

        if ($rowCount > 0){
        //      This will select all the details in the database according to the entered data and the user who entered it
        //      Which will be displayed as a table in the main PHP file.
        $getJobs = "SELECT usertimetrack.*, userlogin.uName, catdb.Catagory, clientdb.Client FROM usertimetrack
                    INNER JOIN userlogin ON usertimetrack.usrId = userlogin.uId
                    INNER JOIN catdb ON usertimetrack.Category = catdb.catId 
                    INNER JOIN clientdb ON usertimetrack.utClient = clientdb.clientId
                    WHERE jDate = :jDate AND usrId = :usrId";
        $getJobsQuery = $dbConnect -> prepare($getJobs);
        $getJobsQuery -> bindParam(':jDate', $_SESSION["date"]);
        $getJobsQuery -> bindParam(':usrId', $_SESSION["uid"]);
        $getJobsQuery -> execute();
        ?>
        <html>
        <head>

            <!--    <!-- Style Sheets -->
            <!--    <link rel="stylesheet" type="text/css" href="../../CSS/main.css">-->
            <!--    <link rel="stylesheet" type="text/css" href="../../CSS/jquery-ui.min.css">-->
            <!---->
            <!--    <!-- Java Scripts -->
            <!--    <script language="JavaScript" type="text/javascript" src="../../jScripts/jquery-3.2.0.min.js"></script>-->
            <!--    <script language="JavaScript" type="text/javascript" src="../../jScripts/jquery-ui.min.js"></script>-->

        </head>
        <body>
        <div id="mainDiv">
            <div id="navi"></div>
            <div id="infoi"></div>
            <table id="hor-minimalist-b" style="table-layout: fixed; width: 96%; margin-left: 2%;">
                <div id="bgDimmer"></div>
                <div id="divContent" style="width: 65%; margin-left: 20%; margin-top: -8%;"></div>
                <thead>
                <tr>
                    <th scope="col" style="width: 6%;">User Name</th>
                    <th scope="col" style="width: 10%;">Category</th>
                    <th scope="col" style="width: 10%;">Client</th>
                    <th scope="col" style="width: 6%;">Start Time</th>
                    <th scope="col" style="width: 6%;">End Time</th>
                    <th scope="col" style="width: 6%;">Time Spent</th>
                    <th scope="col" style="width: 6%;">Volume</th>
                    <th scope="col" style="width: 6%;">Prod. Lines</th>
                    <th scope="col" style="width: 16%;">Remarks</th>
                    <th scope="col" style="width: 10%;"></th>
                </tr>
                </thead>
                <tbody>
                <?php while ($getJobsRow =$getJobsRow = $getJobsQuery -> fetch(PDO::FETCH_ASSOC)) { ?>
                    <tr>
                        <td><?php echo $getJobsRow["uName"]; ?></td>
                        <td><?php echo $getJobsRow["Catagory"]; ?></td>
                        <td style="word-wrap:break-word;"><?php echo $getJobsRow["Client"]; ?></td>
                        <td><?php echo $getJobsRow["startTime"]; ?></td>
                        <td><?php echo $getJobsRow["endTime"]; ?></td>
                        <td><?php echo $getJobsRow["timeSpent"]; ?></td>
                        <td><?php echo $getJobsRow["Volume"]; ?></td>
                        <td><?php echo $getJobsRow["noOfProductLines"]; ?></td>
                        <td style="word-wrap:break-word; text-align: left"><?php echo $getJobsRow["Remarks"]; ?></td>
                        <td>
                            <input type="button" class="utEdit" value="Edit" data-utid="<?php echo $getJobsRow["utId"]; ?>" onmousedown="popup();" />
                            <input type="button" class="utDel" value="Delete" data-utid="<?php echo $getJobsRow["utId"]; ?>" onmousedown="popup();" /></a>
                        </td>
                    </tr>
                <?php }?>
                </tbody>
            </table>
            <?php }else{ ?>
                <span id="noDataMsg">There's no data to display</span>
            <?php }}else{ ?>
            <span id="noDataMsg">User or date not selected</span>
        <?php } ?>
    </div>
</div>
</body>
</html>
我丢失了我写的检索代码,很抱歉(fury delete)在执行blow函数时调用了上述代码段

//Timed process for updating user time entries
function tEditReload() {
    timeSheetAdd();
    setTimeout("setMsg();",100);
    setTimeout("closeForm();",5000);
    setTimeout("resetForm2();",1000);
}

function delTimeProcess() {
    delTimeEntry();
    setTimeout("setMsg();",1000);
    setTimeout("closeForm();",2000);
    setTimeout("resetForm2();",1000);
}
我知道这篇文章很长,但我想清楚地知道我在做什么,基本上我想在重新加载时使用相同的选定搜索变量更新表

如果您直接调用“resetForm2”,则无需在此处创建“onbeforeunload”。只需制作如下内容:

function resetForm2() {
  sessionStorage.date = $('#dPicker').val();
  sessionStorage.uid = $('#uid').val();
  window.location.reload();
}


function getData(ssd,ssid){

    var date = ssd?ssd:document.getElementById("dPicker").value,
        uid = ssid?ssid:document.getElementById("uid").value;

    if (date === ""){
        document.getElementById("noDataMsg").innerHTML = "Date not selected";
    }else if (uid === ""){
        document.getElementById("noDataMsg").innerHTML = "User not selected";
    }else{
        if(window.XMLHttpRequest){

            xmlhttp=new XMLHttpRequest();

        }else{

            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function(){

            if(xmlhttp.readyState==4 && xmlhttp.status==200){

                document.getElementById("resultTable").innerHTML = this.responseText;
            }
        };

        xmlhttp.open("POST","../functions/timeEditResult.php?date="+date+"&uid="+uid,true);
        xmlhttp.send();
    }
}
window.addEventListener("load",()=>if(sessionStorage.date&&sessionStorage.uid)getData(sessionStorage.date,sessionStorage.uid))

没有经过测试,但你应该能够让它工作。

我要回答我自己的问题,因为我的问题确实没有得到答案,这相当长。在发布了我最初的问题后,我等待了48小时,看看是否能得到一个能指引我走上正确道路的答案,但唯一的答案是@Michal Salacinski,谢谢你花时间回答

这就是我所做的(所有的编码都是原创的,是我编造的)

我希望保持搜索条件不变,并用更新的数据加载相同的数据。首先,我尝试使用会话和JavaScript来完成它,这几乎让我抓狂

然后我想到用PHP、MySQL和JavaScript来做,是的,我必须说我成功了

首先,我创建了一个包含所有搜索查询的表,然后创建了一个MySQL任务,每天00点截断该表。然后我将我的主要PHP显示代码一分为二,见下面的代码

<?php if ($_SESSION["sT"] == "Done"){ ?>
<body onload="tEditReload2()">
<div id="divCenter-timeEdit" class="box">
    <label id="sbId" hidden><?php echo $_SESSION["uRole"]?></label>
    <div class="logo-timeEdit">
        <img src="../../images/logo.png" width="142" height="33">
    </div>
    <div id="mainDiv" style="height: 38px;">
        <label for="dPicker">Date:</label>
        <input type="text" id="dPicker" style="margin-left: .5%;" size="10" value="<?php echo $getStermRow["sDate"]; ?>">
        <label for="userSelect" style="margin-left: 2%">Select User:</label>
        <select id="userSelect" style="width:160px; margin-left: .5%;" onchange="usrId(this.id);"></select>
        <input type="text" id="uid" size="1" value="<?php echo $getStermRow["sUid"]; ?>" hidden>
        <input type="button" class="getData" value="Submit" onclick="getData();">
    </div>
    <div id="resultTable"><span id="noDataMsg"></span> </div>
</div>
</body>
<?php }else{?>
<body onload="getUser();">
<div id="divCenter-timeEdit" class="box">
    <label id="sbId" hidden><?php echo $_SESSION["uRole"]?></label>
    <div class="logo-timeEdit">
        <img src="../../images/logo.png" width="142" height="33">
    </div>
    <div id="mainDiv" style="height: 38px;">
        <label for="dPicker">Date:</label>
        <input type="text" id="dPicker" style="margin-left: .5%;" size="10">
        <label for="userSelect" style="margin-left: 2%">Select User:</label>
        <select id="userSelect" style="width:160px; margin-left: .5%;" onchange="usrId(this.id);"></select>
        <input type="text" id="uid" size="1" hidden>
        <input type="button" class="getData" value="Submit" onclick="getData();">
        <input type="button" value="cookie" onclick="readCookie();">
    </div>
    <div id="resultTable"><span id="noDataMsg"></span> </div>
</div>
</body>
<?php } ?>
上面的代码PHP将确保满足正确的
条件,并显示正确的HTML

然后我在body onload中使用了两个函数

  • 这将只加载数据以填充下拉列表,用户可以在其中选择用户名

    函数getUser() { if(window.XMLHttpRequest) { xmlhttp=新的XMLHttpRequest(); } 其他的 { xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”); }

    }

  • 我使用一个定时函数运行两个函数,其中一个加载下拉列表的用户,同时在初始搜索发生时保留所选用户,另一个运行函数从服务器检索搜索数据

  • 定时功能

    function tEditReload2() {
        getUser2();
        setTimeout("getData();",100);
    }
    
    function getData(){
    
        var sbId = document.getElementById("sbId").innerHTML,
            date = document.getElementById("dPicker").value,
            uid = document.getElementById("uid").value;
    
        if (date === ""){
            document.getElementById("noDataMsg").innerHTML = "Date not selected";
        }else if (uid === ""){
            document.getElementById("noDataMsg").innerHTML = "User not selected";
        }else{
            if(window.XMLHttpRequest){
    
                xmlhttp=new XMLHttpRequest();
    
            }else{
    
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange=function(){
    
                if(xmlhttp.readyState==4 && xmlhttp.status==200){
    
                    document.getElementById("resultTable").innerHTML = this.responseText;
                }
            };
    
            xmlhttp.open("POST","../functions/timeEditResult.php?date="+date+"&uid="+uid+"&sbId="+sbId,true);
            xmlhttp.send();
        }
    
    获取数据函数

    function tEditReload2() {
        getUser2();
        setTimeout("getData();",100);
    }
    
    function getData(){
    
        var sbId = document.getElementById("sbId").innerHTML,
            date = document.getElementById("dPicker").value,
            uid = document.getElementById("uid").value;
    
        if (date === ""){
            document.getElementById("noDataMsg").innerHTML = "Date not selected";
        }else if (uid === ""){
            document.getElementById("noDataMsg").innerHTML = "User not selected";
        }else{
            if(window.XMLHttpRequest){
    
                xmlhttp=new XMLHttpRequest();
    
            }else{
    
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange=function(){
    
                if(xmlhttp.readyState==4 && xmlhttp.status==200){
    
                    document.getElementById("resultTable").innerHTML = this.responseText;
                }
            };
    
            xmlhttp.open("POST","../functions/timeEditResult.php?date="+date+"&uid="+uid+"&sbId="+sbId,true);
            xmlhttp.send();
        }
    
    然后使用下面的代码设置PHP代码,将搜索词添加到临时表中,然后更新会话,以便在重新加载时运行正确的HTML,之后,相同的代码将使用内部联接查询从数据库中提取数据

    if ($_REQUEST["date"] != "" && $_REQUEST["uid"] != ""){
    
    //  Add Search terms to search term database to retrieve later
        $addSearch = "INSERT INTO searchterm(sDate,sUid,searchedBy) VALUES (:sDate, :sUid, :sb)";
        $addSearchQuery = $dbConnect -> prepare($addSearch);
        $addSearchQuery -> bindParam(':sDate', $_REQUEST["date"]);
        $addSearchQuery -> bindParam(':sUid', $_REQUEST["uid"]);
        $addSearchQuery -> bindParam(':sb', $_REQUEST["sbId"]);
    
        if ($addSearchQuery -> execute()){
    //      Get's the last entered search ID
            $lastSid = $dbConnect -> lastInsertId();
            $_SESSION["lSid"] = $lastSid;
    
            $_SESSION["sT"] = "Done";
        }else{
            $_SESSION["sT"] = "Not";
        }
    
    //  Check if the there's any data in the database if there is it will be passed on to the query
        $getCount = "SELECT COUNT(*) FROM usertimetrack WHERE jDate = :jDate AND usrId = :usrId";
        $getCountQuery = $dbConnect -> prepare($getCount);
        $getCountQuery -> bindParam(':jDate', $_REQUEST["date"]);
        $getCountQuery -> bindParam(':usrId', $_REQUEST["uid"]);
        $getCountQuery -> execute();
        $rowCount = $getCountQuery -> fetchColumn();
    
        if ($rowCount > 0){
    //      This will select all the details in the database according to the entered data and the user who entered it
    //      Which will be displayed as a table in the main PHP file.
            $getJobs = "SELECT usertimetrack.*, userlogin.uName, catdb.Catagory, clientdb.Client FROM usertimetrack
                        INNER JOIN userlogin ON usertimetrack.usrId = userlogin.uId
                        INNER JOIN catdb ON usertimetrack.Category = catdb.catId 
                        INNER JOIN clientdb ON usertimetrack.utClient = clientdb.clientId
                        WHERE jDate = :jDate AND usrId = :usrId";
            $getJobsQuery = $dbConnect -> prepare($getJobs);
            $getJobsQuery -> bindParam(':jDate', $_REQUEST["date"]);
            $getJobsQuery -> bindParam(':usrId', $_REQUEST["uid"]);
            $getJobsQuery -> execute();
    
    下面的HTML绘制由上述PHP生成的结果表

    <body>
        <div id="mainDiv">
            <div id="navi"></div>
            <div id="infoi"></div>
            <table id="hor-minimalist-b" style="table-layout: fixed; width: 96%; margin-left: 2%;">
                <div id="bgDimmer"></div>
                <div id="divContent" style="width: 65%; margin-left: 20%; margin-top: -8%;"></div>
                <thead>
                <tr>
                    <th scope="col" style="width: 6%;">User Name</th>
                    <th scope="col" style="width: 10%;">Category</th>
                    <th scope="col" style="width: 10%;">Client</th>
                    <th scope="col" style="width: 6%;">Start Time</th>
                    <th scope="col" style="width: 6%;">End Time</th>
                    <th scope="col" style="width: 6%;">Time Spent</th>
                    <th scope="col" style="width: 6%;">Volume</th>
                    <th scope="col" style="width: 6%;">Prod. Lines</th>
                    <th scope="col" style="width: 16%;">Remarks</th>
                    <th scope="col" style="width: 10%;"></th>
                </tr>
                </thead>
                <tbody>
                <?php while ($getJobsRow = $getJobsQuery -> fetch(PDO::FETCH_ASSOC)) { ?>
                    <tr>
                        <td><?php echo $getJobsRow["uName"]; ?></td>
                        <td><?php echo $getJobsRow["Catagory"]; ?></td>
                        <td style="word-wrap:break-word;"><?php echo $getJobsRow["Client"]; ?></td>
                        <td><?php echo $getJobsRow["startTime"]; ?></td>
                        <td><?php echo $getJobsRow["endTime"]; ?></td>
                        <td><?php echo $getJobsRow["timeSpent"]; ?></td>
                        <td><?php echo $getJobsRow["Volume"]; ?></td>
                        <td><?php echo $getJobsRow["noOfProductLines"]; ?></td>
                        <td style="word-wrap:break-word; text-align: left"><?php echo $getJobsRow["Remarks"]; ?></td>
                        <td>
                            <input type="button" class="utEdit" value="Edit" data-utid="<?php echo $getJobsRow["utId"]; ?>" onmousedown="popup();" />
                            <input type="button" class="utDel" value="Delete" data-utid="<?php echo $getJobsRow["utId"]; ?>" onmousedown="popup();" /></a>
                        </td>
                    </tr>
                <?php }?>
                </tbody>
            </table>
            <?php }else{ ?>
                <span id="noDataMsg">There's no data to display</span>
            <?php }}else{ ?>
                <span id="noDataMsg">User or date not selected</span>
            <?php } ?>
        </div>
    </div>
    </body>
    
    添加数据后,我使用上述计时函数获取数据并从数据库检索数据

    我在下拉列表中遇到了一些问题,当页面重新加载时,下拉列表将其自身重置为空项。这是由PHP和javascript一起运行引起的

    因此,我通过编写下面的java脚本并修改从用户登录表中提取数据的PHP,解决了这个问题

    JavaScript

    function getUser2(){
    
        var selUser = document.getElementById("uid").value;
    
        if(window.XMLHttpRequest)
        {
            xmlhttp=new XMLHttpRequest();
        }
        else
        {
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    
        xmlhttp.onreadystatechange=function()
        {
            if(xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                document.getElementById("userSelect").innerHTML=xmlhttp.responseText;
            }
        };
        xmlhttp.open("POST","../functions/getUsrId.php?uid=getUsr2&selUser="+selUser,true);
        xmlhttp.send();
    }
    
    我用
    if.重写的PHP。。否则

    //Get user details from the data base and populate the user select drop down
    if($_REQUEST["uid"] == "getUsr"){
        $getUsr = "SELECT * FROM userlogin ORDER BY uId ASC";
        $getUsrQuery = $dbConnect -> query($getUsr);
    
        echo "<option></option>";
        while ($row = $getUsrQuery -> fetch(PDO::FETCH_ASSOC)){
            echo "<option id= ".$row["uId"].">".$row["fName"]." ".$row["lName"]."</option>";
        }
    }else{
        $getUsr = "SELECT * FROM userlogin ORDER BY uId ASC";
        $getUsrQuery = $dbConnect -> query($getUsr);
    
        $getSelUsr = "SELECT * FROM userlogin WHERE uId = :uid";
        $getSelUsrQuery = $dbConnect -> prepare($getSelUsr);
        $getSelUsrQuery -> bindValue(':uid', $_REQUEST["selUser"]);
        $getSelUsrQuery -> execute();
        $getSelUsrRow = $getSelUsrQuery -> fetch(PDO::FETCH_ASSOC);
    
        echo "<option id= ".$getSelUsrRow["uId"].">".$getSelUsrRow["fName"]." ".$getSelUsrRow["lName"]."</option>";
        while ($row = $getUsrQuery -> fetch(PDO::FETCH_ASSOC)){
            if ($row["uId"] == $_REQUEST["selUser"]) continue;
            echo "<option id= ".$row["uId"].">".$row["fName"]." ".$row["lName"]."</option>";
        }
    }
    
    //从数据库获取用户详细信息并填充用户选择下拉列表
    如果($_请求[“uid”]=“getUsr”){
    $getUsr=“按uId ASC从用户登录订单中选择*”;
    $getUsrQuery=$dbConnect->query($getUsr);
    回声“;
    while($row=$getUsrQuery->fetch(PDO::fetch\u ASSOC)){
    回显“$row[“fName”]。$row[“lName”]。”;
    }
    }否则{
    $getUsr=“按uId ASC从用户登录订单中选择*”;
    $getUsrQuery=$dbConnect->query($getUsr);
    $getSelUsr=“从userlogin中选择*,其中uId=:uId”;
    $getSelUsrQuery=$dbConnect->prepare($getSelUsr);
    $getSelUsrQuery->bindValue(':uid',$\u请求[“selUser”]);
    $getSelUsrQuery->execute();
    $getSelUsrRow=$getSelUsrQuery->fetch(PDO::fetch_ASSOC);
    回显“$GetSelusRow[“fName”]。$GetSelusRow[“lName”]。”;
    while($row=$getUsrQuery->fetch(PDO::fetch\u ASSOC)){
    如果($row[“uId”]=$\u请求[“selUser”])继续;
    回显“$row[“fName”]。$row[“lName”]。”;
    }
    }
    
    做了这些之后,我得到了我想要的(对我来说是的)。我知道这可能不是解决这个问题的最好办法,但我设法让它按我想要的方式工作

    我想知道可以对此做些什么改进,如果有人想使用代码,请随意使用


    我要感谢StackOver flow社区在学习新东西和帮助我解决问题方面提供了很多帮助。

    谢谢你的回答,因为你说我可以设置变量,但函数
    getData()
    在页面重新加载时不会被调用,我将其作为按钮单击事件编写。但你答案的上半部分在我的脑海里点燃了一个小小的LED灯泡,希望它能成长。您能告诉我有没有办法检查会话是否使用java脚本设置。
    if (isset($_SESSION["lSid"])){
        $getSterm = "SELECT searchterm.*, userlogin.fName, userlogin.lName FROM searchterm
                     INNER JOIN userlogin ON searchterm.sUid = userlogin.uId WHERE sId = :sId";
        $getStermQuery = $dbConnect -> prepare($getSterm);
        $getStermQuery -> bindParam(':sId', $_SESSION["lSid"]);
        $getStermQuery -> execute();
        $getStermRow = $getStermQuery -> fetch(PDO::FETCH_ASSOC);
    }
    
    function getUser2(){
    
        var selUser = document.getElementById("uid").value;
    
        if(window.XMLHttpRequest)
        {
            xmlhttp=new XMLHttpRequest();
        }
        else
        {
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    
        xmlhttp.onreadystatechange=function()
        {
            if(xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                document.getElementById("userSelect").innerHTML=xmlhttp.responseText;
            }
        };
        xmlhttp.open("POST","../functions/getUsrId.php?uid=getUsr2&selUser="+selUser,true);
        xmlhttp.send();
    }
    
    //Get user details from the data base and populate the user select drop down
    if($_REQUEST["uid"] == "getUsr"){
        $getUsr = "SELECT * FROM userlogin ORDER BY uId ASC";
        $getUsrQuery = $dbConnect -> query($getUsr);
    
        echo "<option></option>";
        while ($row = $getUsrQuery -> fetch(PDO::FETCH_ASSOC)){
            echo "<option id= ".$row["uId"].">".$row["fName"]." ".$row["lName"]."</option>";
        }
    }else{
        $getUsr = "SELECT * FROM userlogin ORDER BY uId ASC";
        $getUsrQuery = $dbConnect -> query($getUsr);
    
        $getSelUsr = "SELECT * FROM userlogin WHERE uId = :uid";
        $getSelUsrQuery = $dbConnect -> prepare($getSelUsr);
        $getSelUsrQuery -> bindValue(':uid', $_REQUEST["selUser"]);
        $getSelUsrQuery -> execute();
        $getSelUsrRow = $getSelUsrQuery -> fetch(PDO::FETCH_ASSOC);
    
        echo "<option id= ".$getSelUsrRow["uId"].">".$getSelUsrRow["fName"]." ".$getSelUsrRow["lName"]."</option>";
        while ($row = $getUsrQuery -> fetch(PDO::FETCH_ASSOC)){
            if ($row["uId"] == $_REQUEST["selUser"]) continue;
            echo "<option id= ".$row["uId"].">".$row["fName"]." ".$row["lName"]."</option>";
        }
    }