看板 PHP 關於我們 聯絡資訊
用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
kerash:$line = mysql_fetch_assoc($result) ???? 08/21 14:47
kerash:你是要用 mysql_num_rows 嗎? 08/21 14:47
BoxsterX:是的 08/21 14:48
kerash:何不用 while($line=mysql_fetch_assoc()) ? 08/21 14:57
BoxsterX:有試過用while但結果一樣 08/21 14:59
kerash:你改成while後再貼一次程式碼,程式段落都要貼 08/21 15:00
kerash:另外用原始碼看你產生出來的 input 值是否正確 08/21 15:00
※ 編輯: BoxsterX 來自: 1.162.191.133 (08/21 15:09)
BoxsterX:剛試過while了結果還是一樣 08/21 15:10
kerash:我看出來了 ... 你的NAME怎麼都一樣... 08/21 15:26
kerash:要買成name=Tag[] 才會把所有結果一次以陣列方式送出.. 08/21 15:26
BoxsterX:感謝@@ 但我只要傳出按鈕的那一欄值 08/21 15:30
kerash:那你應該不需要 hidden 啊,你直接把 submit 給 name 就好 08/21 15:42
kerash:傳過去就會是按下去的那個 name 的 value 08/21 15:42
BoxsterX:目前都OK了 另外問一下 要怎麼做才能一個button傳多個值 08/21 16:53
kerash:如你這樣做就行了,但是送出的 submit只要一個,其他都是 08/21 16:59
kerash:放在 input 裡面,再傳過去另一頁接收 08/21 17:00
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)