如何在php中显示选定的多个选项 [英] how to show selected multiple options of drop down in php

查看:132
本文介绍了如何在php中显示选定的多个选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先看我的代码。

 < select multiple name ='assign_to []'> 
<
$ result_10 = mysql_query(select * from login_users)或死(mysql_error());
while($ row_10 = mysql_fetch_object($ result_10))
{
echo< option value =。$ row_10-> user_id。>$ row_10->用户名 < /选项> 中。
}
?>
< / select>

当页面提交没有问题时,我可以获得价值,但我的问题是: p>

当页面提交时,我正在重定向到同一页面,然后我希望在提交表单之前选择的选项将在页面提交后看到选择。



在那里写什么PHP代码?为单选下载:

 <? 
while($ row_10 = mysql_fetch_object($ result_10))
{
?>
< option<?= $ row_10-> user_id == $ _ POST ['assign_to']?'selected':''?>值= LT; = $ row_10-> user_id说明> 中>< = $ row_10->用户名>?< /选项>
<
}

?>

希望你得到我的问题?



谢谢!!!

解决方案

  if(in_array($ row_10-> user_id, $ _POST ['assign_to'])){echoselected} else {echo} 

因为你的$ _POST ['assign_to']是一个数组,我猜这可以工作。



或者你可以使用条件运算符:

 < option<?= in_array($ row_10-> user_id,$ _ POST ['assign_to'])? 'selected':''?>值= LT; = $ row_10-> user_id说明> 中>< = $ row_10->用户名>?< /选项> 


First see my code.

   <select multiple name='assign_to[]'>
                    <?
                $result_10 = mysql_query("select * from login_users") or die(mysql_error());
                while($row_10 = mysql_fetch_object($result_10))
                {
                    echo "<option  value=".$row_10->user_id.">".$row_10->username."</option>";
                }
                ?>
    </select>

I am able to get value when page is submitted no issue with this but my problem is :

When page is submitted I am redirecting to the same page then I want that options which are selected before submitting the form will seen selected after page is submitted.

What PHP code is to written there. What I did for single selected drop down as :

<?
while($row_10 = mysql_fetch_object($result_10))
                    {
?>
       <option <?=$row_10->user_id==$_POST['assign_to']?'selected':''?> value="<?=$row_10->user_id?>"><?=$row_10->username?></option>
<?
                    }

?>

Hope you are getting my question ?

Thanks!!!

解决方案

if(in_array($row_10->user_id,$_POST['assign_to'])){echo "selected"}else{echo ""}

As your $_POST['assign_to'] is an array i guess this will work.

OR you can use conditional operators :

<option <?= in_array($row_10->user_id,$_POST['assign_to']) ? 'selected' : ''?> value="<?=$row_10->user_id?>"><?=$row_10->username?></option>

这篇关于如何在php中显示选定的多个选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆