【问题标题】:Getting checkbox text value获取复选框文本值
【发布时间】:2012-08-05 12:29:49
【问题描述】:

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

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>";

这里是屏幕截图

选中复选框的结果

但这就是我想要的

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

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

【问题讨论】:

  • 这很奇怪,因为您为复选框的值和旁边的标签回显了相同的变量 {$row['title']}... 你确定你得到了 $_POST[ 'electiveModules'] 数组值?

标签: php mysql checkbox


【解决方案1】:

这是错误的代码:

foreach(array_keys($aElective) as $elec)

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

foreach($aElective as $elec)

【讨论】:

  • @ivantedja.Perfect !!你是个传奇!!真的很感激!!:)
【解决方案2】:
$aElective = $_POST['electiveModules'];
 foreach( $aElective as $key => $value ) {
    echo "$key: $value";
}

这对你有用吗?

【讨论】:

  • @wkaha。非常感谢。它正在工作。使用预定义函数 array_keys() 导致了问题。感谢您的建议,祝您有美好的一天!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多