Home > Backend Development > PHP Tutorial > Why is the value of this session always the last value?

Why is the value of this session always the last value?

WBOY
Release: 2016-07-06 13:52:55
Original
1448 people have browsed it

In fact, I want to save the value in the first box using the session so that I don’t have to re-enter it when an error is prompted.
But every time my code saves the value, it is the value when the last input error was made. why is that? For example, when I input 11 for the first time, the second box intentionally outputted it. When I input 22 for the second time, the second box also intentionally entered the wrong input. Why is the value in the first box 11 at this time? If you keep trying like this, the number displayed will always be the last number. What should I do?

<code><form method="post">
    <input type="text" autocomplete="off" value="<?php session_start(); if(isset($_SESSION["name"])){echo $_SESSION["name"];}  ?>" name="name"><br /><br />
    <input type="text" autocomplete="off"  name="age"><br /><br />
    <button type="submit">提交</button>
</form>
<?php
if(isset($_POST['age'])){
    $_SESSION["name"]=$_POST['name'];
    if($_POST['age']==2){
      echo "正确";
      $_SESSION["name"]='';
    }else{
      echo "错误";
    }
}
?></code>
Copy after login
Copy after login

Reply content:

In fact, I want to save the value in the first box using the session so that I don’t have to re-enter it when an error is prompted.
But every time my code saves the value, it is the value when the last input error was made. why is that? For example, when I input 11 for the first time, the second box intentionally outputted it. When I input 22 for the second time, the second box also intentionally entered the wrong input. Why is the value in the first box 11 at this time? If you keep trying like this, the number displayed will always be the last number. What should I do?

<code><form method="post">
    <input type="text" autocomplete="off" value="<?php session_start(); if(isset($_SESSION["name"])){echo $_SESSION["name"];}  ?>" name="name"><br /><br />
    <input type="text" autocomplete="off"  name="age"><br /><br />
    <button type="submit">提交</button>
</form>
<?php
if(isset($_POST['age'])){
    $_SESSION["name"]=$_POST['name'];
    if($_POST['age']==2){
      echo "正确";
      $_SESSION["name"]='';
    }else{
      echo "错误";
    }
}
?></code>
Copy after login
Copy after login

Change to this:

<code><?php
 session_start();
if(isset($_POST['age'])){
    $_SESSION["name"]=$_POST['name'];
    if($_POST['age']==2){
      echo "正确";
      $_SESSION["name"]='';
    }else{
      echo "错误";
    }
}
?>
<form method="post">
    <input type="text" autocomplete="off" value="<?php if(isset($_SESSION["name"])){echo $_SESSION["name"];}  ?>" name="name"><br /><br />
    <input type="text" autocomplete="off"  name="age"><br /><br />
    <button type="submit">提交</button>
</form></code>
Copy after login

From your description, it should be that in the subsequent operation "waiting for the second input of 22", you did not update the corresponding value of session in name, so she still saved it for the first time11

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template