如何在php中显示选定的多个选项 [英] how to show selected multiple options of drop down in 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屋!