dongling4288 2013-07-11 17:27
浏览 69

将提供一个选择选项的下拉列表更改为复选框中的多个选项

I'm modifying a form and is something I've never actually had the experience in working with. For the record I don't code PHP.

In short: the html/php form has a drop down of five values (fields of research) of which you can only select one. But the client would like to be able to select more than one. So now I would like to trash the drop down and have checkboxes instead.

I would prefer to do the least mods to what is currently set-up.

Note* This is a dynamic dropdown list; so the values are pulled from another table and submitted to the database.

Here's the code I'm working with:

$query_fields_of_research = "SELECT * FROM tblOpportunityFieldOfResearch ORDER BY      
field_of_research_en ASC;";
$sql->query($query_fields_of_research, SQL_ALL, SQL_ASSOC);
$fields_of_research = $sql->record;
?>
<select name="strFieldOfStudy_en" id="strFieldOfStudy_en">
   <?php
   foreach ($fields_of_research as $field) {
       $selectedValue= "";  
       if ($strFieldOfStudy_en == $field['id'])
           $selectedValue= " selected";
       echo "\t\t\t".'<option 
       value="'.$field['id'].'"'.$selectedValue.'>'.$field['field_of_research_en']."
       </option>
";
   }
   ?>
</select>

Following this, the submitted data is also updated to a webpage in a table with this:

<tr>
<td>Fields of research</td>
<td>
<?php 
$fieldsOfResearch = array('1', '2', '3', '4', '5');
if (in_array($opp['strfieldofstudy_en'], $fieldsOfResearch)){
    echo $iri->getFieldOfResearch($opp['strfieldofstudy_en'], 'en');
}
else
    echo $opp['strfieldofstudy_en'];
?></td>
</tr>

How would I change the dropdown to checkboxes with the least amount of mods?

  • 写回答

1条回答 默认 最新

  • duanhe8280 2013-07-11 18:19
    关注

    You can do this

    In html page with select box

    Replace :

    <select name="strFieldOfStudy_en" id="strFieldOfStudy_en">
       <?php
       foreach ($fields_of_research as $field) {
           $selectedValue= "";  
           if ($strFieldOfStudy_en == $field['id'])
               $selectedValue= " selected";
           echo "\t\t\t".'<option 
           value="'.$field['id'].'"'.$selectedValue.'>'.$field['field_of_research_en']."
           </option>
    ";
       }
       ?>
    </select>
    

    By:

    <?php
           $i = 1;
           foreach ($fields_of_research as $field) {
               $selectedValue= "";  
               if ($strFieldOfStudy_en == $field['id'])
                   $selectedValue= 'checked="checked"';
              echo '<checkbox id="strFieldOfStudy_en'.$i.'" name="strFieldOfStudy_en[]"    
              value="'.$field['id'].'"'.$selectedValue.'>'.$field['field_of_research_en'];
    
               $i++;
           }
    ?>
    

    In the submit page:

    Replace:

        if (in_array($opp['strfieldofstudy_en'], $fieldsOfResearch)){
          echo $iri->getFieldOfResearch($opp['strfieldofstudy_en'], 'en');
        }
        else
         echo $opp['strfieldofstudy_en'];
    

    By:

    foreach($_POST['strFieldOfStudy_en'] as $item) {
            if (in_array($item, $fieldsOfResearch)){
               echo $iri->getFieldOfResearch($item, 'en');
            }
            else
              echo $opp['strfieldofstudy_en'];
    }
    // here $_POST['strFieldOfStudy_en'] includes all the values of checkbox checked in   
    //the html form
    
    // foreach extracts each value from $_POST['strFieldOfStudy_en'] into $item(say ids
    //1,2,3,...).
    

    I hope this can be of some help

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图