繁体   English   中英

将 PHP 变量传递给其他页面

[英]Passing PHP Variables to other pages

我有一个表单,用户可以在其中输入详细信息,结果显示在同一页面上。 然后可以提交这些结果。

First_page.php

<?php
session_start();

if (isset($_POST['applySubmit'])) { 
    $_SESSION['amountdiv'] = $amountdiv;
    $_SESSION['fixedrate'] = $fixedrate;
    $_SESSION['deliverydiv'] = $deliverydiv;
    $_SESSION['date'] = $date;  
    $_SESSION['total'] = $total;    
    $_SESSION['grandtotal'] = $grandtotal;      
    $_SESSION['reference'] = $reference;
}
?>

我已经看过很多关于这个的例子,我正在使用这篇文章中的这个例子PHP Pass variable to next page 我的问题是在第二页上,我想输出详细信息,但什么也没有出现。

Second_page.php

<?php
session_start();
//*************Session form details to carry through*****************//
$amountdiv = $_SESSION['amountdiv'];
$fixedrate = $_SESSION['fixedrate'];
$deliverydiv = $_SESSION['deliverydiv'];
$date = $_SESSION['date'];  
$total = $_SESSION['total'];    
$grandtotal = $_SESSION['grandtotal'];
$reference = $_SESSION['reference'];
?> 

然后我想将此信息回显到屏幕上,但什么也没有出现。

<td class="blaah1"><?php echo $_SESSION['amountdiv'] ;?></td>
<td class="blaah1"><?php echo $_SESSION['grandtotal'] ;?></td>
<td class="blaah3"><?php echo $_SESSION['date'] ; ?></td>

为什么什么都没有出现?

这里 :

if (isset($_POST['applySubmit'])) { 
    $_SESSION['amountdiv'] = $amountdiv;
    $_SESSION['fixedrate'] = $fixedrate;
    $_SESSION['deliverydiv'] = $deliverydiv;
    $_SESSION['date'] = $date;  
    $_SESSION['total'] = $total;    
    $_SESSION['grandtotal'] = $grandtotal;      
    $_SESSION['reference'] = $reference;
}

你在哪里为这些变量赋值: $amountdiv$fixedrate ... 所有变量看起来都是空的。

其次,检查您的代码if在第一页的if条件中执行。

<form method="POST" action="Second_page.php">
    <input type="text" name="amountdiv" />
    <input type="text" name="fixedrate" />
    <input type="text" name="deliverydiv" />
    <input type="text" name="date" />
    <input type="text" name="total" />
    <input type="text" name="grandtotal" />
    <input type="text" name="reference" />
    <input type="submit" name="applySubmit" />
</form>

第二页.php

<?php
   echo "<pre>";
   print_r($_POST);
   echo "</pre>";
?>

<td class="blaah1"><?php echo $_POST['amountdiv'] ;?></td>
<td class="blaah1"><?php echo $_POST['grandtotal'] ;?></td>
<td class="blaah3"><?php echo $_POST['date'] ; ?></td>

您需要为这些变量分配一些东西。 目前,看起来您在$amountdiv分配任何内容之前正在使用它,因此它将为null

<?php
session_start();

if (isset($_POST['applySubmit'])) { 
    $amountdiv = $_POST['applySubmit'];
    $_SESSION['amountdiv'] = $amountdiv;

但实际上,这一切都可以一步完成:

<?php
session_start();

if (isset($_POST['applySubmit'])) { 
    $_SESSION['amountdiv'] = $_POST['amountdiv'];

显然,您还应该验证和清理输入。 例如

<?php
session_start();

if (isset($_POST['applySubmit'])) { 
    $_SESSION['amountdiv'] = (int)$_POST['amountdiv'];

或者使用filter_var()filter_input()或您自己的自定义过滤器/清理函数。

首页.php

  <?php
    session_start();

    extract($_POST);

    if (isset($_POST['applySubmit'])) { 
        $_SESSION['amountdiv'] = $amountdiv;
        $_SESSION['fixedrate'] = $fixedrate;
        $_SESSION['deliverydiv'] = $deliverydiv;
        $_SESSION['date'] = $date;  
        $_SESSION['total'] = $total;    
        $_SESSION['grandtotal'] = $grandtotal;      
        $_SESSION['reference'] = $reference;
    }
    ?>
  1. session_start() 前没有空格。
  2. 并使用这个 'extract($_POST);' 在 firstpage.php => 其工作正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM