获取复选框文本值

时间:2012-08-05 12:29:49

标签: php mysql checkbox

我在获取数据库填充复选框的文本值时遇到了一些问题。我的查询根据我的查询填充复选框。

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)

因此,当我选中几个复选框并按提交时,它会传递我选中的复选框的数值,但我想要文本值。对此有何帮助?

2 个答案:

答案 0 :(得分:1)

这是错误的代码:

foreach(array_keys($aElective) as $elec)

因为你使用array_keys,它将得到索引而不是值, 它应该是:

foreach($aElective as $elec)

答案 1 :(得分:0)

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

这对你有用吗?