dongluanguang4774
2017-07-19 09:23
浏览 58
已采纳

在php中重置会话变量

I have the following form on "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>

The "order.php" looks like this:

<?php
require 'connection.php';
session_start();
if(isset($_SESSION['ph']))
    echo ($_SESSION['ph']);
?>

The first time I load the "test.php" and input the phone number it works perfectly and gives me the correct output on "order.php", but the second time onward, "order.php" gives me the same value which I had entered the first time even though I input a different value. I refreshed the page, same result.

I closed the file and reloaded it, still same value. Why is it behaving that way and how do I correct it? I want session to change value whenever a new number is entered which is not happening.

图片转代码服务由CSDN问答提供 功能建议

我在“test.php”上有以下表格。

 <  code>&lt;?php 
session_start(); 
if(isset($ _ POST ['ph']))
 if(isset($ _ POST ['submit']))
 $ _SESSION ['ph'] =  $ _POST ['ph']; 
?&gt; 
&lt;!doctype html&gt; 
&lt; html lang =“en”&gt; 
&lt; body&gt;  
&lt; form method =“POST”action =“order.php”id =“custphoneform”&gt; 
&lt; label for =“PhoneNumber”&gt;输入电话号码:&lt; / label&gt; 
&lt;输入 type =“number”name =“ph”required&gt; 
&lt; input type =“submit”value =“Submit”name =“submit”&gt; 
&lt; / form&gt; 
&lt; / body&gt; 
&lt;  ; / html&gt; 
   
 
 

“order.php”如下所示:

 &lt;?php \  nrequire'connection.php'; 
session_start(); 
if(isset($ _ SESSION ['ph']))
 echo($ _SESSION ['ph']); 
?&gt; 
    
 
 

我第一次加载“test.php”并输入完美的电话号码并在命令下给出正确的输出。 php“,但是第二次,”order.php“给了我第一次输入的相同值,即使我输入了不同的值。 我刷新了页面,结果相同。

我关闭了文件并重新加载,仍然是相同的值。 为什么它表现那样,我该如何纠正呢? 我希望会话在输入未输入的新号码时更改值。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douzhengnao8265 2017-07-19 09:25
    已采纳

    Change the new value to SESSION ON your order.php page like below:-

    <?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']);  
    }
    ?>
    

    Also change test.php code like this:-

    <?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>
    
    打赏 评论

相关推荐 更多相似问题