iT邦幫忙

0

php與Select Option 的複存方式

php
  • 分享至 

  • xImage

嗨!請問一個下拉式選單點選後要存取其資料,那麼在不知道有多少資料的形況下,要如何存取?

}
?>

  $_GET[i]=$_GET['morning'];  <<-----行得通嗎?
  echo $_GET[i]; 

  $i++;
 
}

?>

fillano iT邦超人 1 級 ‧ 2016-06-16 10:41:06 檢舉
貼code貼壞了?
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

2
fillano
iT邦超人 1 級 ‧ 2016-06-16 11:10:51

寫一個php如下:

<?php
    echo nl2br(print_r($_GET, true));

假設php程式url是localhost/test1004.php,透過瀏覽器這樣打上get參數:

http://localhost/test1004.php?morning[]=1&morning[]=2&morning[]=5

你會看到如下內容:

Array
(
[morning] => Array
(
[0] => 1
[1] => 2
[2] => 5
)

)

傳進多個morning[]為名稱的參數,在php的$_GET['morning']就會得到一個陣列,在post也是一樣。select跟checkbox都可以多選,但是處理方式是一致的。

不過你的程式貼上可能不完整,所以看不太出來你怎麼取值,但是絕對不會是$_GET[$i],用for迴圈存取應該用$_GET['morning'][$i]才對。

0
wiseguy
iT邦超人 1 級 ‧ 2016-06-20 00:47:58

不管網頁上的 <option> 有多少個,網頁 form 只會把選中那個傳給 php,所以你的 php 根本不必 care 有幾個 <option>,就是抓 <select name="xxx"> 那個 $_POST['xxx'] 或 $_GET['xxx'] 即可。

我要發表回答

立即登入回答