php 检查是否选择了选择选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14648363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:42:00  来源:igfitidea点击:

Check if select option selected

phpformsselect

提问by user1632298

I have a form where I post some options to a mysql query. When it returns, it a draw a table with some data.

我有一个表格,我在其中发布了一些 mysql 查询选项。当它返回时,它会绘制一个包含一些数据的表格。

How to set the select box's option to be selected if my form returned?

如果我的表单返回,如何设置选择框的选项?

I tried this, but not working:

我试过这个,但不起作用:

<form method="post" id="partnersearchform">
<input type="hidden" name="formaction" value="partnersearch">
Partner Típus<br>
<select onchange="document.getElementById('partnersearchform').submit();" size="" name="ceg">
<option value="">(mind)</option>
<option value="1" if($ceg==1){ print 'selected'; }>Magánszemélyek</option>
<option value="2" if($ceg==2){ print 'selected'; }>Cégek</option>
</select>
</form>

回答by v0d1ch

Try it like this:

像这样尝试:

<?php if($ceg==1){ echo 'selected="selected"'; } ?>

Also make sure what you get in $ceg var.

还要确保你在 $ceg var 中得到了什么。

<option value="<?php echo $ceg;?>"
 <?php if($ceg==1){ echo 'selected="selected"'; }?>>Magánszemélyek</option>

回答by Carlos Campderrós

You forgot to open and close php tags:

您忘记打开和关闭 php 标签:

<form method="post" id="partnersearchform">
<input type="hidden" name="formaction" value="partnersearch">
Partner Típus<br>
<select onchange="document.getElementById('partnersearchform').submit();" size="" name="ceg">
<option value="">(mind)</option>
<option value="1"<?php if($ceg==1){ print ' selected'; }?>>Magánszemélyek</option>
<option value="2"<?php if($ceg==2){ print ' selected'; }?>>Cégek</option>
</select>
</form>

回答by Jimzie

<option value="1" <?php if($ceg==1){ print 'selected'; }?> >Magánszemélyek</option>