会话变量无法访问不同的页面

时间:2015-09-27 14:01:04

标签: php session

我遇到了使会话变量在不同页面上工作的问题

我有三个PHP脚本 1. index.php(包括html表单)

<?php
session_start();

if (isset($_POST['submit'])) {
$_SESSION['billTo_email'] = $_POST['billTo_email'];

}
?>
<form id = 'form_submit'  method="post" action = "NetworkOnline.php">
<td><input type="text" name="billTo_email" id="f_name2" /></td>
<td><input type="submit" name="submit" id="submit" value="Submit"/></td>
</form>
?>
  1. NetworkOnline.php

      <?php
      session_start();
      $_SESSION['billTo_email'] = $_POST["billTo_email"];
      ?> 
    
      $f_name=$_POST["f_name"];
      $l_name=$_POST["l_name"];
      $billTo_country=$_POST["billTo_country"];
      $billTo_city=$_POST["billTo_city"];
      $billTo_state=$_POST["billTo_state"];
      $billTo_street1=$_POST["billTo_street1"];
      $billTo_pin=$_POST["billTo_pin"];
      $billTo_email  = $_SESSION["billTo_email"];
    
  2. response.php

    <?php
    session_start();
    $_SESSION["billTo_email"] = $_POST["billTo_email"];
    ?>
    
    <form name = "form1" method = "post" action="mail.php" >
    <input type="hidden" name="order" id="order" value="<?php echo $order ?>" >
    <input type="hidden" name="bank" id="bank" value="<?php echo $bank ?>" >
    <input type="hidden" name="amount" id="amount" value="<?php echo $amount ?>" >
    <input type = "test" name= "email" id ="email" value= "<?php echo $billTo_email ?>">
    <input type="submit" id="submit" value="submit" name="submit">
    </form>
    

3 个答案:

答案 0 :(得分:0)

试试这个, 的index.php:

<html>
<form id = 'form_submit'  method="post" action = "NetworkOnline.php">
<td><input type="text" name="billTo_email" /></td>
<td><input type="submit" name="submit" id="submit" value="Submit"/></td>
</form>
</html>

NetworkOnline.php:

<?php
session_start();
$_SESSION['billTo_email'] = $_POST["billTo_email"];
header('Location: response.php');
?>

response.php:

<?php
session_start();
$billTo_email=$_SESSION["billTo_email"] 
?>
<form name = "form1" method = "post" action="mail.php" >
<br />
<br />
<input type = "test" name= "email"  value= "<?php echo $billTo_email ?>">
<input type="submit" id="submit" value="submit" name="submit">
</form>

答案 1 :(得分:-1)

尝试以下,

你的结束标签错误,

<强>的index.php

<?php
session_start();

if (isset($_POST['submit'])) {
$_SESSION['billTo_email'] = $_POST['billTo_email'];
}
?>
<form id = 'form_submit'  method="post" action = "NetworkOnline.php">
<td><input type="text" name="billTo_email" id="f_name2" /></td>
<td><input type="submit" name="submit" id="submit" value="Submit"/></td>
</form>

<强> NetworkOnline.php

  <?php
  session_start();

  $_SESSION['billTo_email'] = $_POST["billTo_email"];
  $f_name=$_POST["f_name"];
  $l_name=$_POST["l_name"];
  $billTo_country=$_POST["billTo_country"];
  $billTo_city=$_POST["billTo_city"];
  $billTo_state=$_POST["billTo_state"];
  $billTo_street1=$_POST["billTo_street1"];
  $billTo_pin=$_POST["billTo_pin"];
  $billTo_email  = $_SESSION["billTo_email"];
  ?> 

<强> response.php

<?php
session_start();
$_SESSION["billTo_email"] = $_POST["billTo_email"];
?>

<form name = "form1" method = "post" action="mail.php" >
<input type="hidden" name="order" id="order" value="<?php echo $order; ?>" >
<input type="hidden" name="bank" id="bank" value="<?php echo $bank; ?>" >
<input type="hidden" name="amount" id="amount" value="<?php echo $amount; ?>" >
<input type = "test" name= "email" id ="email" value= "<?php echo $billTo_email; ?>">
<input type="submit" id="submit" value="submit" name="submit">
</form>

答案 2 :(得分:-1)

<强>更新

你的 index.php

<form id = 'form_submit'  method="post" action = "NetworkOnline.php">
  <td><input type="text" name="billTo_email" id="f_name2" /></td>
  <td><input type="submit" name="submit" id="submit" value="Submit"/></td>
</form>

注意:此处无需检查if (isset($_POST['submit'])) { ... },因为 index.php 将数据发送到 NetworkOnline.php

您的 NetworkOnline.php

<?php
  ob_start();   // turn on output buffering
  session_start();
  if( isset( $_POST['billTo_email'] ) ) { // work only if **billTo_email** has some value
     $_SESSION['billTo_email'] = $_POST["billTo_email"];
  }

  //  ?> <-- remove this closing tag

  $f_name=$_POST["f_name"]; // this line of code shows undefined variable, 
         //because in your index.php there is no any f_name field presnet.

  // your remaining codes


 ?>