[英]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;
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.