Reputation: 561
I am having troubling getting a simply if/else conditional for options in an html select form. If the value for bid is 'N', then I want one option for the select field to change to "Y" and vice versa. 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.
/* 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>
<?
}
Upvotes: 0
Views: 15046
Reputation: 1496
Your condition is bad, you must remove echo
and ;
. 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>
Upvotes: 0
Reputation: 18592
if
statement expect expression and you have syntax error in your statement
if (echo $row['bid']; == 'N')
the syntax error because of echo
and the semicolon ;
you should change your condition to
if ($row['bid'] == 'N'){
if $row['bid']
string then you need to compare with strcmp
, like this
if (strcmp($row['bid'] , 'N') == 0){
also you need to move <input>
tag outside the <select>
Upvotes: 2
Reputation: 5708
Your <input type="submit">
was inside your <select>
, so I moved it outside.
It is also incorrect to use the if
statement in this manner:
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>
Upvotes: 1
Reputation: 312
Your if condition has bad syntax. Remove the semicolon and echo statement.
<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>
Upvotes: -2