duanhe0817825
2018-08-04 04:52
采纳率: 100%
浏览 118
已采纳

如何在多个选择中保留所选值,如bootstrap-select

I want to retain the value that I selected after form submission.

<select name="student[]" class="selectpicker" multiple title="Click here.." data-width="auto" data-live-search="true" required>
  <?php  
    if (isset($_POST['submit'])){
    $selected = $_POST['studname'];
    $qry = "Select studtbl.stud_id,concat(studtbl.fname,' ', 
    substring(studtbl.mname, 1,1),'. ',studtbl.lname) as Name from studtbl";
        $result = mysqli_query($conn, $qry);
        while($row = mysqli_fetch_array($result))
        {
        extract($row);
        echo '<option value="'.$stud_id.'" '.(($stud_id == 
        $selected)? 'selected="selected"':"" >'.$Name.'</option>';
        } 
    }else{
    $qry = "Select studtbl.stud_id,concat(studtbl.fname,' ', 
    substring(studtbl.mname, 1,1),'. ',studtbl.lname) as Name from studtbl";
                $result = mysqli_query($conn, $qry);
                while($row = mysqli_fetch_array($result))
                {
                    extract($row);
                    echo '<option value="'.$stud_id.'" >'.$Name.'</option>';
                } 
    }
    ?>
</select>
<input type='submit' class="btn btn-primary" name='submit' value='Create 
     Account' />

My problem is it is not retaining after submitting

</div>

图片转代码服务由CSDN问答提供 功能建议

我想保留表单提交后选择的值。

 &lt; select name =”student []“class =”selectpicker“multiple title =”点击此处 ..“data-width =”auto“data-live-search =”true“required&gt; 
 
&lt;?php 
 
 if(isset($ _ POST ['submit'])){
 \  n $ selected = $ _POST ['studname']; 
 
 $ qry =“选择studtbl.stud_id,concat(studtbl.fname,'',
 
 substring(studtbl.mname,1,1),'  。',studtbl.lname)作为姓名来自studtbl“; 
 
 $ result = mysqli_query($ conn,$ qry); 
 
 while($ row = mysqli_fetch_array($ result))
 
 {\  r 
 extract($ row); 
 
 echo'&lt; option value =“'。$ stud_id。'”'。(($ stud_id == 
 
 $ selected)?'selected =“selected”  ':“”&gt;'。$ Name。'&lt; / option&gt;'; 
 
} 
 
}其他{
 
 $ qry =“选择studtbl。  stud_id,concat(studtbl.fname,'',
 
 substring(studtbl.mname,1,1),'。  ',studtbl.lname)作为姓名来自studtbl“; 
 
 $ result = mysqli_query($ conn,$ qry); 
 
 while($ row = mysqli_fetch_array($ result))
 
 {
 
  
 extract($ row); 
 
 echo'&lt; option value =“'。$ stud_id。'”&gt;'。$ Name。'&lt; / option&gt;'; 
 
} 
 
 \  n} 
 
?&gt; 
 
&lt; / select&gt; 
 
&lt; input type ='submit'class =“btn btn-primary”name ='submit'value ='Create 
 
帐户 '/&gt;   
 
  
 
  
 
 
 
 

我的问题是它在提交后没有保留

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • dongyi6269 2018-08-04 08:37
    最佳回答
    <select name="student[]" class="selectpicker" multiple title="Click here.." data-width="auto" data-live-search="true" required>
      <?php     
        if (isset($_POST['submit'])){
        $selected = $_POST['student']; // <---- multi-selects come in as Array
        $qry = "Select studtbl.stud_id,concat(studtbl.fname,' ', 
        substring(studtbl.mname, 1,1),'. ',studtbl.lname) as Name from studtbl";
            $result = mysqli_query($conn, $qry);
            while($row = mysqli_fetch_array($result))
            {
            extract($row);
            // if the current ID is inside the POST-ed Array - mark as SELECTED
            echo '<option value="'.$stud_id.'" '.(in_array($stud_id, $selected) ? 'selected="selected"': "").'>'.$Name.'</option>';
            } 
        }else{
        $qry = "Select studtbl.stud_id,concat(studtbl.fname,' ', 
        substring(studtbl.mname, 1,1),'. ',studtbl.lname) as Name from studtbl";
                    $result = mysqli_query($conn, $qry);
                    while($row = mysqli_fetch_array($result))
                    {
                        extract($row);
                        echo '<option value="'.$stud_id.'" >'.$Name.'</option>';
                    } 
        }
        ?>
    </select>
    <input type='submit' class="btn btn-primary" name='submit' value='Create 
         Account' />
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题