php - 获取复选框文本值

标签 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)

因此,当我选中几个复选框并按下提交时,它会传递我选中的复选框的数值,但我想要的是文本值。请问有什么帮助吗?

最佳答案

这是错误的代码:

foreach(array_keys($aElective) as $elec)

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

foreach($aElective as $elec)

关于php - 获取复选框文本值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11816417/

相关文章:

angular - 复选框不返回假 Angular 2

html - 将复选框和其他两个元素对齐,就像它们是一条线一样

html - 如何选择之前的标签并选中复选框?

php - CSS 背景图片未加载,似乎找不到图片

php - PHP中@符号的用途是什么?

javascript - 在 WordPress 中包含 typed.js

PHP PDO 获取所有表

javascript - 我无法从 php 打开模式

mysql - 查询保留未减去的元组

php - 通过 URL 为 Angular View 发送 ID