iT邦幫忙

0

PHP表單傳送多變數!!!

php

Dear IT大大~
有一個需求,想請大大們幫忙...
我想從我第一頁按一個button按鈕傳送多變數到下一頁,我的作法是把變數全部連結成一串,在自行拆解成自己要的字數改成變數...
請問各位大大有什麼更好的方法嗎?麻煩大大指導小弟...

<table width="1068" border="3">

  <tr>
    <td align="center">工號</td>
	<td align="center">季</td>
    <td align="center">中文姓名</td>
	<td align="center">英文姓名</td>
	<td align="center">部門</td>
    <td align="center">遲到</td>
    <td align="center">早退</td>
    <td align="center">曠職</td>
    <td align="center">事假</td>
	<td align="center">病假</td>
	<td align="center">留職停薪</td>
	<td align="center">大功</td>
	<td align="center">小功</td>
	<td align="center">嘉獎</td>
	<td align="center">優點</td>
	<td align="center">大過</td>
	<td align="center">小過</td>
	<td align="center">申誡</td>
	<td align="center">缺點</td>
	<td align="center">test1</td>
	<td align="center">test2</td>
  </tr>
  
<?php
for($i=1;$i<=mysql_num_rows($data);$i++){
$rs=mysql_fetch_row($data);
?>

  <tr>
    <td align="center"><?php echo $rs[0]?></td>
	<td align="center"><?php echo $rs[1]?></td>
    <td align="center"><?php echo $rs[2]?></td>
    <td align="center"><?php echo $rs[3]?></td>
    <td align="center"><?php echo $rs[4]?></td>
    <td align="center"><?php echo $rs[5]?></td>
    <td align="center"><?php echo $rs[6]?></td>
	<td align="center"><?php echo $rs[7]?></td>
    <td align="center"><?php echo $rs[8]?></td>
	<td align="center"><?php echo $rs[9]?></td>
	<td align="center"><?php echo $rs[10]?></td>
	<td align="center"><?php echo $rs[11]?></td>
	<td align="center"><?php echo $rs[12]?></td>
	<td align="center"><?php echo $rs[13]?></td>
	<td align="center"><?php echo $rs[14]?></td>
	<td align="center"><?php echo $rs[15]?></td>
	<td align="center"><?php echo $rs[16]?></td>
	<td align="center"><?php echo $rs[17]?></td>
	<td align="center"><?php echo $rs[18]?></td>
	<td align="center"><?php echo '<input type="button" name="id" value="表單" onclick=window.open("detail.php?workno='.$rs[0].''.$rs[1].''.$rs[2].'")>'?></td>	                                                                                
	<td align="center"><button><a style="text-decoration:none;" href="detail.php?workno=<?php echo $rs[0] ?>"target="_blank"><?php echo $rs[0] ?></a></button></td> 
  </tr>



  
<?php
}
?>
你從資料庫取出來的東西沒有「唯一識別碼」嗎?
有識別碼的話,丟這個值過去再撈資料不就好了?
好吧!假如你認真的覺得要這樣丟資料。
不如封裝成JSON字串格式丟過去……
再來就是mysql()指令集已廢止,請愛用PDO
但如果你還是堅持在舊版php使用mysql()指令集。
也請不要使用mysql_fetch_row()這種根本找麻煩的東西。
小哈 iT邦新手 4 級 ‧ 2017-02-09 15:37:09 檢舉
好...我會努力開始學PDO 感謝Samと可樂快跑大大
tsengs iT邦新手 4 級 ‧ 2022-11-26 15:37:05 檢舉
讚!!!!!
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

1
海綿寶寶
iT邦大神 1 級 ‧ 2017-02-08 15:12:17
最佳解答

參考資料來源

1.先寫出最終 URL
detail.php?workno=12345&season=spring&cname=楊丞琳

2.包進 php 中,變成
<td align="center"><?php echo '<input type="button" name="id" value="表單" onclick=window.open("detail.php?workno=12345&season=spring&cname=楊丞琳")>'?></td>

3.把數值改成變數名稱,變成
<td align="center"><?php echo '<input type="button" name="id" value="表單" onclick=window.open("detail.php?workno='.$rs[0].'&season='.$rs[1].'&cname='.$rs[2].'")>'?></td>

4.收工放飯

5.detail.php裡取得參數
$workno = $_GET["workno"];
$season = $_GET["season"];
$cname = $_GET["cname"];
看更多先前的回應...收起先前的回應...
小哈 iT邦新手 4 級 ‧ 2017-02-08 16:08:13 檢舉

Dear 海綿大大~
果然行得通,實在太感謝...
現在都在優化程式的部分,感謝海綿大執導^^
還有一個問題想問...
就是我Echo $season = $_GET["season"];變數
如果是2017Q1 就抓成上一季的2016Q4 以此類推!!!
是否用我以下寫的方式可以達成,或者是有更好的方式> <

if ($harry==201701 or $harry==201702 or $harry==201703)
{$ha="2016Q4";}
else if ($harry==201704 or $harry==201705 or $harry==201706)
{$ha="2017Q1";}
else if ($harry==201707 or $harry==201708 or $harry==201709)
{$ha="2017Q2";}
else if ($harry==201710 or $harry==201711 or $harry==201712)
{$ha="2017Q3";}
//2018年
else if ($harry==201801 or $harry==201802 or $harry==201803)
{$ha="2017Q4";}
else if ($harry==201804 or $harry==201805 or $harry==201806)
{$ha="2018Q1";}
else if ($harry==201807 or $harry==201808 or $harry==201809)
{$ha="2018Q2";}
else if ($harry==201810 or $harry==201811 or $harry==201812)
{$ha="2018Q3";}
wonton iT邦高手 6 級 ‧ 2017-02-09 10:40:11 檢舉
switch (substr($harry, 4, 2)) {
    case '01': case '02': case '03':
        $ha = (substr($harry, 0, 4) - 1)."Q4"; break;
    case '04': case '05': case '06':
        $ha = substr($harry, 0, 4)."Q1"; break;
    case '07': case '08': case '09':
        $ha = substr($harry, 0, 4)."Q2"; break;
    case '10': case '11': case '12':
        $ha = substr($harry, 0, 4)."Q3"; break;
}

沒地方替 wonton 大大的回答按讚(差點按到檢舉),只好在這裡按
/images/emoticon/emoticon12.gif/images/emoticon/emoticon12.gif/images/emoticon/emoticon12.gif

小哈 iT邦新手 4 級 ‧ 2017-02-09 15:36:30 檢舉

感謝wonton 大大的解答!!!!

0
pratnket
iT邦新手 5 級 ‧ 2017-02-14 20:33:53
A頁
###cart.php###

說明
<input type="text" name="YourName">
name = POST的名稱

大概就會變下面這樣
$_POST['YourName'];

form表單
<form action="cart_data.php?a=href" method="post">
POST的頁面 => action="cart_data.php?a=href"
?a是GET['a']變數

html語法
<form action="cart_data.php?a=href" method="post">
 文字輸入欄位: <td align="center"><input type="test" name="YourName"></td>
 <input type="submit" value="送出表單">
</form>

B頁
###cart_data.php###

PHP語法
if($_GET[a]=="href"){
    $SQL_INPUT_ARRAY = array();
    $SQL_INPUT_ARRAY = trim($_Post) ;
    //串接為字串
    $Fields = implode(",",array_keys($SQL_INPUT_ARRAY)); 
    $Values = implode("','",$SQL_INPUT_ARRAY); 
    //新增資料
    $SQL = "INSERT INTO cart ({$Fields}) VALUES ('{$Values}')";		
    $DB->query($SQL,0) ;
}
資料庫結構
id	name	birthday	math	english	history	total
1	李大華	1997/3/7	90	94	92	276
2	陳小明	1997/2/1	82	88	90	260
3	劉小珍	1997/8/3	89	87	78	254

懂了以後...下面你就會看懂了

純程式碼頁

A頁-----------------------------------------------------
###cart.php###
html語法
<form action="cart_data.php?a=href" method="post">
    <td>
         <input type="test" name="YourName">
    </td>
 <input type="submit" value="送出表單">
</form>

B頁-----------------------------------------------------
###cart_data.php###

陣列轉變數
foreach ($_POST as $Key => $Info) {
    echo $Info["變數"];
}
如果有要對資料進行拿取...

if($_GET[a]=="href"){
    //宣告空陣列
    $SQL_INPUT_ARRAY = array();
    //放入陣列
    $SQL_INPUT_ARRAY = trim($_Post) ;
    //串接為字串索引值
    $Fields = implode(",",array_keys($SQL_INPUT_ARRAY)); 
    //串接為字串數值
    $Values = implode("','",$SQL_INPUT_ARRAY); 
    //新增資料
    $SQL = "INSERT INTO cart ({$Fields}) VALUES ('{$Values}')";		
    //存入資料庫,$DB是我寫的物件導向
    $DB->query($SQL,0) ;
}

我要發表回答

立即登入回答