我在“test.php”上有以下表格。
<?php
session_start();
if(isset($_POST['ph']))
if(isset($_POST['submit']))
$_SESSION['ph'] = $_POST['ph'];
?>
<!doctype html>
<html lang="en">
<body>
<form method="POST" action="order.php" id="custphoneform">
<label for="PhoneNumber">Enter Phone Number:</label>
<input type="number" name="ph" required>
<input type="submit" value="Submit" name="submit">
</form>
</body>
</html>
“order.php”如下所示:
<?php
require 'connection.php';
session_start();
if(isset($_SESSION['ph']))
echo ($_SESSION['ph']);
?>
我第一次加载"test.php"
并输入电话号码时效果很好,并在"order.php"
上给出了正确的输出,但第二次,"order.php"
给了我同样的信息即使我输入了不同的值,我第一次输入的值。我刷新了页面,结果相同。
我关闭了文件并重新加载,仍然是相同的值。为什么它表现那样,我该如何纠正呢?我希望会话在输入未输入的新号码时更改值。
答案 0 :(得分:1)
将新值更改为SESSION
页面上的order.php
,如下所示: -
<?php
require 'connection.php';
session_start();
if(!empty($_POST['ph'])){
$_SESSION['ph'] = $_POST['ph']; //change value of phonenumber inside SESSION
}
if(!empty($_SESSION['ph'])){
echo ($_SESSION['ph']);
}
?>
同样更改test.php
代码: -
<?php
session_start(); // no need to do other stuff
?>
<!doctype html>
<html lang="en">
<body>
<form method="POST" action="order.php" id="custphoneform">
<label for="PhoneNumber">Enter Phone Number:</label>
<input type="number" name="ph" required>
<input type="submit" value="Submit" name="submit">
</form>
</body>
</html>