簡體   English   中英

在Javascript中分配會話變量

[英]Assigning session variables in Javascript

每當嘗試觸發事件時,我都試圖分配一個會話變量(在此處使用Javascript)。 但是我未能實現它,因此它僅在頁面正文加載后加載,而不是像按下按鈕那樣生成after事件。

我有兩頁:一是檢查是否設置了會話var。 如果是,則執行該操作並銷毀它。

另一個是一個巨大的頁面,其中包含它作為子頁面(使用include),並且它是一種表單,提交給自身,使其重新加載。 現在,無論何時生成事件(例如,按下提交按鈕,然后單擊按鈕),該子頁面都將設置會話。

first.php->會話檢查並銷毀second.php->會話初始化和分配

兩者都是迭代的。

我可以使用后端來存儲數據,但是這個大頁面有一些限制,不能在其中包含其他數據庫。

first.php

<?php
    session_start();
    print_r($_SESSION);
    var_dump($_SESSION);

    if(isset($_SESSION['submit']) && isset($_SESSION['click']) ) {
        echo "passed both tests";
        unset($_SESSION['submit']);
        unset($_SESSION['click']);
        session_destroy():
    }
    var_dump($_SESSION);
    print_r($_SESSION);
    var_dump($_SESSION);
?>

second.php(窗體)

<?php
    session_start();
?>
<html>
  <head>
    <meta name="generator" content="HTML Tidy for Windows (vers 14 February 2006), see www.w3.org">
    <script type="text/javascript">
        function submit_valid(){
            <?php
                $_SESSION['submit']='yes';
            ?>

        }
    </script>
    <script type="text/javascript">
        function click_valid1(){
            document.getElementById('submitButton').disabled=true;
            <?php
                $_SESSION['click']='clicked';
            ?>

            window.location.replace("first.php");
        }
    </script>
    <script type="text/javascript">
        function load_valid3(){
            <?php
                $_SESSION['submit']='no';
                $_SESSION['clicked']='not';
            ?>
        }
    </script>

    <script type="text/javascript">
        setTimeout (function(){
            document.getElementById('submitButton').disabled = null;
        },10000);
        var countdownNum = 10;
        incTimer();
        function incTimer(){
            setTimeout (function(){
                if(countdownNum != 0){
                    countdownNum--;
                    document.getElementById('timeLeft').innerHTML = 'Time left: ' + countdownNum + ' seconds';
                    incTimer();
                } else {
                    document.getElementById('timeLeft').innerHTML = 'Ready!';
                }
            },1000);
        }
    </script>

    <title></title>
  </head>
  <body onload="return load_valid3()">
    <form action="search.php" method="get" onsubmit="return submit_valid()">
      <table border="2" width="150" cellpadding="0" cellspacing="2">
        <tr>
          <td align="center">
            <input type="text" id="query" name="query" size="30" value="">
          </td>
          <td align="center">
            <input type="submit" value="Search"> <input type="hidden" name="search" value="1">
          </td>
        </tr>
        <tr>
          <td align="center">
            <input type="button" value="SUBMIT" disabled="disabled" id="submitButton" onclick="return click_valid1()">
          </td>
          <td align="center">
            <p id="timeLeft">
              Time Left: 10 seconds
            </p>
          </td>
        </tr>
      </table>
    </form>
  </body>
</html>

PHP中有靜態變量的概念嗎? 這樣我就可以擺脫這種情況。

嘗試將JavaScript函數調用與表單提交操作分開。 您可以將“ isset函數”與“ if條件語句”一起使用,以檢查是否提交了表單。 就像是:

if(isset($_GET['submit']))
{

}

盡管不知道該怎么做:

我使用jQuery示例性地實現了您的第一個JS函數“ valid() ”:

function valid() {
    $.ajax({
        type: "POST",
        url: "first.php",
        data: {
            "color": "red"
        }
    });
}

這是更新會話變量的方式。

$_SESSION['color'] = $_REQUEST['color']

我仍然沒有真正看到“有效”功能的意義,但是也許這向您顯示了可以去的地方。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM