简体   繁体   中英

Assigning session variables in Javascript

I am trying to assign a session variable whenever an event is triggered (using Javascript here). But I'm failing to make it happen so that it's loaded just after the body of my page loaded, not the after event generated like pressing a button.

I have two pages: one is to check wether the session var is set. If yes, then perform the action and destroy it.

Another one is a huge page in which it is included as a sub page (using include), and it is a form, which is submitted to itself, making it reload. Now that subpage will have set a session whenever an event is generated (like pressing a submit button, and clicking a button).

first.php --> session checking and destroying second.php --> session initialisation and assigning

And both are iterative.

I can use a back-end to store the data but that huge page has some restrictions not to include other dbs inside it.

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

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

Is there any concept of static variables in PHP? So that I can get rid of this situation.

Try to seperate javascript function calls from the form submit action. You may use "isset function" with "if condition statements" to check whether the form is submitted or not. Something like:

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

}

Despite not really knowing where you are going with this:

Using jQuery I exemplary implemented your first JS-function " valid() ":

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

This is how you update the session variable.

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

I still do not really see what the point of the "valid" functions are but maybe this shows you where you could go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM