问题
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;
?>
回答1:
$_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']
)
回答2:
its because
$_SESSION['off']=$_SESSION['off']=+1;
^
+---unwanted = sign
it should be
$_SESSION['off']=$_SESSION['off']+1;
also same apply to the $_SESSION['on']
回答3:
$_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
回答4:
Declare your session variable inside your HTML PAGE in the first php tag and it should solve your problem.
来源:https://stackoverflow.com/questions/16153396/session-variable-increments-only-once