2012-08-05 176 views
1

我有點問題獲取數據庫填充複選框的文本值。下面是我的查詢,根據我的查詢填充複選框。獲取複選框的文本值

if (isset($_POST['submitCourseCode'])) { 

    $aElective = $_POST['electiveModules']; 
    foreach(array_keys($aElective) as $elec) { 
    echo "$elec"; 
    } 
} 
echo "<form name=\"psform\" action=\"plotyourcourseGraphpSave.php\" method=\"post\">"; 
$moduleQuery = "SELECT module.*,group_elective_modules.moduleID 
       FROM module,group_elective_modules 
       WHERE group_elective_modules.courseName = '$courseTitle' 
       AND group_elective_modules.yr = '$year' 
       AND group_elective_modules.moduleID = module.ID "; 

$moduleResult = mysql_query($moduleQuery); 
while ($row = mysql_fetch_array($moduleResult)) { 

    echo "<input type=\"checkbox\" name=\"electiveModules[]\" value=\"{$row['title']}\" /> {$row['title']}<br />";              
}                                 
echo "<input type=\"submit\" name=\"submitCourseCode\" value=\"Submit\" /> 
</form>"; 

下面是屏幕截圖 enter image description here

結果中選中的複選框

enter image description here

但是,這就是我想要

Threshold French 
French for Reading Purposes I 
German Language (Beginner [00] Level) 
German Language (Intermediate [05] Level) 

所以,當我選擇幾的ch eck框和我按提交,它通過我選擇的複選框的數值,但我想要的文本值。對此有任何幫助嗎?

+0

這很奇怪,因爲你回聲相同的變量{$ row ['標題']}複選框的值和旁邊的標籤...你確定你有$ _POST ['electiveModules']數組價值? – Samson 2012-08-05 12:59:34

回答

1

這是不正確的代碼:

foreach(array_keys($aElective) as $elec) 

因爲你用array_keys,它將獲得該指數的,而不是價值, 應該是:

foreach($aElective as $elec)

+0

@ ivantedja.Perfect !!你是傳奇!真的很感謝!! :) – user1444442 2012-08-05 13:10:58

0
$aElective = $_POST['electiveModules']; 
foreach($aElective as $key => $value) { 
    echo "$key: $value"; 
} 

是這對你有用嗎?

+0

@ wkaha.Thanks很多,它現在工作。使用預定義的函數array_keys()導致了這個問題。感謝您的建議,祝您有美好的一天! – user1444442 2012-08-05 13:23:07