简体   繁体   中英

session variable increments only once

I am using session values to increment 2 variable across pages with ajax..Simple one.. I have used session_start in Page1 and initialize the session variable to 0 in page 1.. In page2 I am initializing them based on condition.. I seems to increment once, them there is no effect..Here is my action page..

    <?php 
session_start(); 
?>
<html>
<body>
<?php

$j=isset($_POST['commit']) && $_POST['commit'];
    if($j)

    {
    $num=rand(0, 100);
    ?>


<div>
    <div id="inner_1">
        <?php 
        if($num<27){ 
            echo "Result:Hit";
            $_SESSION['off']=$_SESSION['off']=+1;
                echo "Offensive Player:";
            if(isset($_SESSION['off'])){echo $_SESSION['off'];}else{echo '0';}
                echo "Defensive Player:";
             if(isset($_SESSION['on'])){echo $_SESSION['on'];}else{echo '0';} 
                } 
        else 
                {
            echo "Result:Miss";
            $_SESSION['on']=$_SESSION['on']=+1;
            echo "Offensive Player:";
            if(isset($_SESSION['off'])){echo $_SESSION['off'];}else{echo '0';}
            echo "Defensive Player:";
            if(isset($_SESSION['on'])){echo $_SESSION['on'];}else{echo '0';}
                }    ?>
    </div>


</div>



    <?php } ?>

</body>
</html>

Page1

        <?php
       session_start();
       $_SESSION['off']=0;
       $_SESSION['on']=0;
       ?>
$_SESSION['on']=$_SESSION['on']=+1;

What should this do? assign twice +1 to $_SESSION['on'] ?

To add some number to a variable, use:

$_SESSION['on'] += 1;

(Do the same for your $_SESSION['off'] )

its because

$_SESSION['off']=$_SESSION['off']=+1;
                                 ^
                                 +---unwanted = sign 

it should be

 $_SESSION['off']=$_SESSION['off']+1;

also same apply to the $_SESSION['on']

$_SESSION['off']=+1 means it assign value 1 to session variable

Change

$_SESSION['off']=$_SESSION['off']=+1;

to

$_SESSION['off'] = $_SESSION['off']+1;

also change same as $_SESSION['on']=$_SESSION['on']=+1; above

在第一个php标签中的HTML PAGE中声明会话变量,它应该可以解决您的问题。

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