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