[請益] 用POST傳遞的array的值永遠是最後一項..

看板PHP作者時間13年前 (2012/08/21 14:33), 編輯推噓1(1015)
留言16則, 2人參與, 最新討論串1/4 (看更多)
用for把SQL裡的資料列出來並在後面加上一個按鈕傳遞值 code如下(只列出重點) <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Snapshot List</title> </head> <body bgcolor="#FFFFFF" text="#000000"> <?php $link = mysql_connect("127.0.0.1", "root", "1234") or die("Could not connect : " . mysql_error()); mysql_select_db("FT_info") or die("Could not select database"); $query="Select * from list"; $result = mysql_query($query) or die("Query failed : " . mysql_error()); $alumni_id=$line['serialno']; mysql_close($link); ?> <form action="http://127.0.0.1/site/tag.php" method="POST"> <?php for ($i=0;$line = mysql_fetch_assoc($result);$i++) { $tag[$i] = $line["Tag"]; echo "<td>".'<input type="hidden" name="Tag" value='.$tag[$i].'><input type="submit" value='."$tag[$i]".'>'."</td>\n"; } ?> while版 <?php while ($line = mysql_fetch_assoc($result)) { echo "<td>".'<input type="hidden" name="Tag" value='. $line["Tag"];.'><input type="submit" value='. $line["Tag"].'>'."</td>\n"; } ?> </form> </body> </html> tag.php code如下 <?php echo "debug msg: "; echo $_POST["Tag"]; ?> 按鈕顯示的值是正常的 但不管按哪個button送出去的值一直都是array的最後一個值 請求各位幫忙 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 1.162.191.133

08/21 14:47, , 1F
$line = mysql_fetch_assoc($result) ????
08/21 14:47, 1F

08/21 14:47, , 2F
你是要用 mysql_num_rows 嗎?
08/21 14:47, 2F

08/21 14:48, , 3F
是的
08/21 14:48, 3F

08/21 14:57, , 4F
何不用 while($line=mysql_fetch_assoc()) ?
08/21 14:57, 4F

08/21 14:59, , 5F
有試過用while但結果一樣
08/21 14:59, 5F

08/21 15:00, , 6F
你改成while後再貼一次程式碼,程式段落都要貼
08/21 15:00, 6F

08/21 15:00, , 7F
另外用原始碼看你產生出來的 input 值是否正確
08/21 15:00, 7F
※ 編輯: BoxsterX 來自: 1.162.191.133 (08/21 15:09)

08/21 15:10, , 8F
剛試過while了結果還是一樣
08/21 15:10, 8F

08/21 15:26, , 9F
我看出來了 ... 你的NAME怎麼都一樣...
08/21 15:26, 9F

08/21 15:26, , 10F
要買成name=Tag[] 才會把所有結果一次以陣列方式送出..
08/21 15:26, 10F

08/21 15:30, , 11F
感謝@@ 但我只要傳出按鈕的那一欄值
08/21 15:30, 11F

08/21 15:42, , 12F
那你應該不需要 hidden 啊,你直接把 submit 給 name 就好
08/21 15:42, 12F

08/21 15:42, , 13F
傳過去就會是按下去的那個 name 的 value
08/21 15:42, 13F

08/21 16:53, , 14F
目前都OK了 另外問一下 要怎麼做才能一個button傳多個值
08/21 16:53, 14F

08/21 16:59, , 15F
如你這樣做就行了,但是送出的 submit只要一個,其他都是
08/21 16:59, 15F

08/21 17:00, , 16F
放在 input 裡面,再傳過去另一頁接收
08/21 17:00, 16F
name後面有括號 用 print_R($_POST) 接收會變成print出所有我在DB裡的東西 name後面沒括號 用'<input type="hidden" name="re_snapshotTag"value='.$line["snapshotTag"].'> 接收:echo $_POST["re_snapshotTag"]; 在單一input是可以正確的print出我在 button上的值 但用了多個input會回到我最一開的問題他只會print出array最後的值 所以我想請問能不能用一個button送出多個值並print出正確的值 code: echo "<td>".'<input type="hidden" name="re_snapshotTag[]" value='.$line["snapshotTag"].'> <input type="hidden" name="count[]" value='.$i.'> <input type="hidden" name="re_imagePath[]" value='.$line["imagePath"].'> <input type="submit" value="Restore"> '."</td>\n"; ※ 編輯: BoxsterX 來自: 36.228.19.164 (08/21 18:08)
文章代碼(AID): #1GCokj_v (PHP)
文章代碼(AID): #1GCokj_v (PHP)