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

时间:2017-06-09 08:38:09

标签: javascript php jquery

我正在为我正在努力的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>

我的JavaScript

function getData(){

    var 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,true);
        xmlhttp.send();
    }
}

我的函数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>

然后我使用此表来编辑或删除存在问题的列出的数据。一旦删除或编辑主表保持不变,我想刷新页面并更新现有表而不重做初始日期和用户选择。我确实尝试了以下在搜索后学到的代码,但我认为我做得不对。

重新加载JavaScript

function resetForm2() {
    justBeforeReload();
    window.location.reload();

}

function justBeforeReload() {
    window.onbeforeunload = function () {
        sessionStorage.setItem("date", $('#dPicker').val());
        sessionStorage.setItem("date", $('#uid').val());
    }
}

我遗漏了我写的对不起的检索代码(愤怒删除)执行打击功能时调用上面的代码片段。

//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);
}

我知道帖子很长但想要清楚地知道我想要做什么基本上我想在重新加载时使用相同的选定搜索变量更新表。

请来一个请给我看灯。

2 个答案:

答案 0 :(得分:0)

如果您正在呼叫&#34; resetForm2&#34;直接,没有必要创建&#34; onbeforeunload&#34;这里。只需要做出类似的事情:

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

未经测试但您应该能够正常使用。

答案 1 :(得分:0)

我将回答我自己的问题,因为我真的没有得到一个相当长的问题的答案。在发布我最初的问题之后,我等了48个小时,看看我是否得到了一个答案,这个答案会引导我走上正确的道路,但只有@Michal Salacinski发布了答案,谢谢你抽出时间回答。

所以这就是我所做的(所有编码都是原创的,并且由我制作完毕)。

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

然后我想到用PHP,MySQL和JavaScript做这件事,是的,我想说我成功了。

我首先做的是,我创建了一个表,它将保存所有搜索查询,然后创建一个MySQL任务,每天在00.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 } ?>

以下是我用来为$_SESSION["sT"]设置初始会话的代码。

if (!isset($_SESSION["sT"])){
    $_SESSION["sT"] = "No";
}

以上代码PHP将确保满足正确的if条件并显示正确的HTML。

然后我在我的身体上使用了2个函数。

  1. 只会加载数据以填充用户选择用户名的下拉列表。

    function getUser() {     如果(window.XMLHttpRequest)     {         xmlhttp = new XMLHttpRequest();     }     其他     {         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=getUsr",true);
    xmlhttp.send();
    

    }

  2. 我使用定时函数来运行2个函数,其中一个函数在初始搜索发生时保持所选用户的同时加载用户以进行下拉,另一个函数运行该函数以从服务器检索搜索数据。

  3. 定时功能,

    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>
    

    现在我遇到了填充下拉列表和日期选择器文本区域的问题,因此创建了以下代码部分。

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

    添加数据后,我使用上面提到的定时函数来获取数据并从数据库中检索数据。

    当我重新加载页面时,我遇到了一个问题,即将自身重置为空项目。这是由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 .. else重写的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>";
        }
    }
    

    这样做之后,我得到了我想要的东西(对我来说)。我知道这可能不是解决这个问题的最佳方法,但我设法让它以我想要的方式运行。

    我想知道可以对此做些哪些改进,也就是说如果有人想使用该代码请随意使用。

    我要感谢StackOver流程社区帮助我学习新东西并帮助我解决问题。