我有一个表单,用户输入详细信息,结果显示在同一页面上。然后可以提交这些结果。
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>
为什么没有出现?
答案 0 :(得分:0)
这里:
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
条件中执行。
答案 1 :(得分:0)
<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>
<强> Second_page.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>
答案 2 :(得分:0)
您需要为这些变量指定一些内容。目前,在您为其分配任何内容之前,您似乎正在使用$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'];
显然,您还应该验证并清理输入。 e.g。
<?php
session_start();
if (isset($_POST['applySubmit'])) {
$_SESSION['amountdiv'] = (int)$_POST['amountdiv'];
或使用filter_var()
,filter_input()
或您自己的自定义过滤器/清除功能。
答案 3 :(得分:-1)
Firstpage.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;
}
?>