[請益] 利用Session變數製作跨頁表單的問題
最近被老闆交代做一個跨頁表單,
亦即有多個頁面、每個頁面有一個表單,
同一個使用者填完每個表單後,再一次將數據結果記錄在MySQL資料庫裡,
以下是我簡化後的例子:
版本:PHP 5.2.6
頁面:Test.php --> 有一個表單選項按鈕 (value = 1 or 2, 命名為Choice)
Test2.php --> 有一個表單選項按鈕 (value = 1 or 2, 命名為Choice2)
資料表:Result (有兩個欄位,分別記錄 Choice 與 Choice2 的結果)
目前我打算利用 Session 變數,
將第一頁的表單結果存在一個 Session 變數裡,
之後到第二頁後,再與第二頁的結果一起記錄到資料庫的資料表中,
我的程式碼如下:
//Test.php
<form action="Test2.php" method="POST" name="form1" id="form1">
<label>
<input name="Choice" type="radio" value="1" />
A</label>
<label>
<input name="Choice" type="radio" value="2" />
B</label>
<input type="submit" name="Submit" value="送出"/>
</form>
//Test2.php
<?php require_once('Connections/Connect1.php'); ?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "",
$theNotDefinedValue = "")
{
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" :
"NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
session_start();
$_SESSION['Choice'] = $_POST['Choice'];
echo $_SESSION['Choice'];
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO Result (Choice, Choice2) VALUES (%s, %s)",
GetSQLValueString($_SESSION['Choice'], "int"),
GetSQLValueString($_POST['Choice2'], "int"));
mysql_select_db($database_Connect1, $Connect1);
$Result1 = mysql_query($insertSQL, $Connect1) or die(mysql_error());
$insertGoTo = "index.html";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
?>
<form action="<?php echo $editFormAction; ?>" method="POST" name="form1">
<label></label>
<p>
<label>
<input name="Choice2" type="radio" value="1">
C</label>
<label>
<input name="Choice2" type="radio" value="2">
D</label>
</p>
<p>
<label>
<input type="submit" name="Submit" value="送出">
</label>
</p>
<input type="hidden" name="MM_insert" value="form1">
</form>
為了測試 Session 變數是否生效,我特別下了 echo 指令來測試 (紅色字體的部分),
結果發現該 Session 的確會顯示 Test.php 輸入的結果,
但又出現以下錯誤:
Notice: Undefined index: Choice in C:\Program Files\Apache Software
Foundation\Apache2.2\htdocs\Test2.php on line 29
Column 'Choice' cannot be null
(line 29為黃色字體那一行)
這裡我百思不得其解,
既然 echo 已經說明 Choice 輸入結果確實存在於 Session 變數裡,
但為何又說我沒有對 Choice 下定義?
為了再測試自己是不是 Session 指令有寫錯,
我將黃色字體的部分改為:$_SESSION['Choice'] = 1; //強迫輸入為1
再重新跑一次就完全正常了,
所以可見應該是出在 $_POST['Choice'] 寫法的問題,
但我一直想不到該如何解決 ...
我才剛入門 PHP 而已,
很多地方還不是那麼熟悉,也不知道這樣做對不對 @@~
不知道是否有先進能伸出援手?萬分感激~
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 123.193.188.19
推
05/21 13:15, , 1F
05/21 13:15, 1F
→
05/21 13:19, , 2F
05/21 13:19, 2F
推
05/21 13:32, , 3F
05/21 13:32, 3F
→
05/21 13:32, , 4F
05/21 13:32, 4F
→
05/21 13:52, , 5F
05/21 13:52, 5F
→
05/21 13:52, , 6F
05/21 13:52, 6F
推
05/21 15:33, , 7F
05/21 15:33, 7F
→
05/22 10:25, , 8F
05/22 10:25, 8F
→
05/22 10:26, , 9F
05/22 10:26, 9F
→
05/22 11:13, , 10F
05/22 11:13, 10F
→
05/22 11:14, , 11F
05/22 11:14, 11F
→
05/22 11:15, , 12F
05/22 11:15, 12F
→
05/22 11:33, , 13F
05/22 11:33, 13F
→
05/22 23:21, , 14F
05/22 23:21, 14F
→
05/23 20:18, , 15F
05/23 20:18, 15F
→
05/23 20:19, , 16F
05/23 20:19, 16F
→
05/23 20:20, , 17F
05/23 20:20, 17F
→
05/23 20:47, , 18F
05/23 20:47, 18F
→
05/23 20:48, , 19F
05/23 20:48, 19F
→
05/23 22:25, , 20F
05/23 22:25, 20F
討論串 (同標題文章)
以下文章回應了本文 (最舊先):
完整討論串 (本文為第 1 之 3 篇):
PHP 近期熱門文章
PTT數位生活區 即時熱門文章