[英]If/else for conditional <select> option
I am having troubling getting a simply if/else conditional for options in an html select form. 我在为HTML选择表单中的选项获取简单的if / else条件感到麻烦。 If the value for bid is 'N', then I want one option for the select field to change to "Y" and vice versa.
如果出价值为“ N”,那么我希望选择字段的一个选项更改为“ Y”,反之亦然。 I know my db connection is good and other php works and the form was updating the db fine until I tried to work in the php conditional code within the so I just need to get the syntax correct.
我知道我的数据库连接良好,其他php都可以正常工作,并且表单可以正常更新db,直到我尝试在php条件代码中工作,所以我只需要获取正确的语法即可。
/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)){
?>
<form method="post" name="update" action="update.php" >
<select name="bid">
<option value="<?php echo $row['bid']; ?>" selected="selected">
<?php echo $row['bid']; ?></option>
<? if ($row['bid'] == 'N') {
?>
<option value="Y">Y</option>
<? } else {
?>
<option value="N">N</option>
<? }
?>
</select>
<input type="submit" name="Submit" value="update" >
</form>
<?
}
if
statement expect expression and you have syntax error in your statement if
语句期望表达式,并且语句中有语法错误
if (echo $row['bid']; == 'N')
the syntax error because of echo
and the semicolon ;
由于
echo
和分号而导致的语法错误;
you should change your condition to 您应该将条件更改为
if ($row['bid'] == 'N'){
if $row['bid']
string then you need to compare with strcmp
, like this 如果
$row['bid']
字符串,则需要与strcmp
进行比较,像这样
if (strcmp($row['bid'] , 'N') == 0){
also you need to move <input>
tag outside the <select>
您还需要将
<input>
标记移到<select>
Your <input type="submit">
was inside your <select>
, so I moved it outside. 您的
<input type="submit">
在<select>
里面,所以我将其移到了外面。
It is also incorrect to use the if
statement in this manner: 以这种方式使用
if
语句也是不正确的:
if (echo $row['bid']; == 'N')
correct would be this: 正确的是这样的:
if ($row['bid'] == 'N')
Here is the corrected code: 这是更正的代码:
<form method="post" name="update" action="update.php" >
<select name="bid">
<?php
echo '<option value="'.$row['bid'].'" selected="selected">'.$row['bid'].'</option>';
if ($row['bid'] == 'N') echo '<option value="Y">Y</option>';
else echo '<option value="N">N</option>';
echo '</select>';
?>
<input type="submit" name="Submit" value="update" >
</form>
Your condition is bad, you must remove echo
and ;
您的条件不好,必须删除
echo
和;
. 。 it should like this
它应该像这样
<? if ($row['bid'] == 'N') { ?>
And your submit button should be presented out side the select field. 并且您的提交按钮应显示在选择字段之外。 Your codes should look like this:
您的代码应如下所示:
<form method="post" name="update" action="update.php" >
<select name="bid">
<option value="<?=$row['bid']?>" selected="selected">
<?=$row['bid']?>
</option>
<?php if ($row['bid'] == 'N') {
echo '<option value="Y">Y</option>';
} else {
echo '<option value="N">N</option>';
}
?>
</select>
<input type="submit" name="Submit" value="update" >
</form>
Your if condition has bad syntax. 您的if条件语法错误。 Remove the semicolon and echo statement.
删除分号和echo语句。
<form method="post" name="update" action="update.php" >
<select name="bid">
<option value="<?php echo $row['bid']; ?>" selected="selected">
<?php echo $row['bid']; ?></option>
<? if ($row['bid'] == 'N') {
?>
<option value="Y">Y</option>
<? } else {
?>
<option value="N">N</option>
<? }
?>
<input type="submit" name="Submit" value="update" >
</select>
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.