作者edlin ()
看板PHP
標題[請益] 利用Session變數製作跨頁表單的問題
時間Wed May 21 13:07:52 2008
最近被老闆交代做一個跨頁表單,
亦即有多個頁面、每個頁面有一個表單,
同一個使用者填完每個表單後,再一次將數據結果記錄在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
1F:推 chadjye:要不要把session_start();移到程式的第一行試試看 05/21 13:15
2F:→ edlin:剛剛試過了還是一樣不成功 >< 05/21 13:19
3F:推 arrack:session_register('Choice'); 05/21 13:32
4F:→ arrack:那只是NOTICE不是錯誤 05/21 13:32
5F:→ edlin:感謝說明~ 不過新版的PHP語法不是已經不用寫register嗎? 05/21 13:52
6F:→ edlin:我剛剛試了一次結果還是相同 ... 05/21 13:52
7F:推 JoeHorn:好像是數值沒傳過去? 把 $_POST 倒出來看看~ 05/21 15:33
8F:→ edlin:我用echo $_POST['Choice']來看,裡面也有我先前輸入的數值 05/22 10:25
9F:→ edlin:好像牽涉記錄在資料庫就會失效...是php.ini要額外設定嗎 @@~ 05/22 10:26
10F:→ starjou:php 版本? 05/22 11:13
11F:→ starjou:上一行請無視,那個 notice 是不是你直接開 test2.php 時 05/22 11:14
12F:→ starjou:才有? 05/22 11:15
13F:→ edlin:Notice是出現在Test2.php選完並按送出後 05/22 11:33
14F:→ starjou:你的 test2.php form裡的變數叫Choice2 $_POST裡沒 Choice 05/22 23:21
15F:→ edlin:我是將Choice寫入Session變數後,在Test2裡送出這個Session 05/23 20:18
16F:→ edlin:如程式碼中亮白色的部分 ... 還是這種寫法本來就不行? 05/23 20:19
17F:→ edlin:但文中我也提到Session裡是直接指定的常數的話,是可行的 05/23 20:20
18F:→ starjou:session 裡的變數就叫 $_SESSION 了,不會再叫 $_POST 05/23 20:47
19F:→ starjou:除非你在 test2.php 用 hidden input 再重指定一次 05/23 20:48
20F:→ edlin:真是一語驚醒夢中人!感謝樓上先進的建議~ 05/23 22:25