Selected Value Get from Db into Dropdown Select Box Option Using PHP MySQL Error

How to show selected value from database in dropdown using Laravel?

This is a example of how I do this:

<select class="js-states browser-default select2" name="shopping_id" required id="shopping_id">
<option value="option_select" disabled selected>Shoppings</option>
@foreach($shoppings as $shopping)
<option value="{{ $shopping->id }}" {{$company->shopping_id == $shopping->id ? 'selected' : ''}}>{{ $shopping->fantasyname}}</option>
@endforeach
</select>

Cannot set the selected values in a drop down box from a PHP function

Your errors here are the following.

First, you need to call opponent_load(Wheathill) after closing >. Currently, it is invalid markup, browser tries to fix it, but it is still invalid.

<select id ="Opponents" name ="Opponents">
<?php opponent_load(Wheathill); ?>
</select>

Second, as you pass a string to a function, it's reasonable to enclose string in quotes.

<select id ="Opponents" name ="Opponents">
<?php opponent_load('Wheathill'); ?>
</select>

And third one - is outputing <option>:

echo "<option value='$uName' '$selected'> $uName </option>";

If you put your $selected = 'selected="selected"'; here you will get:

<option value='$uName' 'selected="selected"'> $uName </option>

That's definitely an invalid markup. The solution is - remove quotes:

echo "<option value='$uName' $selected> $uName </option>";

which becomes:

<option value='$uName' selected="selected"> $uName </option>

which is correct.

select option value from database on selected

Focusing on this one area, you should be getting a PHP error unless it is a copy error. Notice the following changes:

while($rows=mysql_fetch_assoc($query)){
?>
<form method=\"POST\" action=\"edit.php\">
<input type ='hidden' name='ID' value = '<?php echo $rows['ID'];?>'>
<select name ="pnames">
<?php foreach ($arrayproducts as $key => $value) {
?>
<option value = "<?php echo $key; ?>"
<?php
if ($key == $productName){
echo 'selected="selected"';
}
?> >
<?php echo $value; ?>
</option>
<?php } //end foreach ?>
</select>
<?php }//end while ?>

You had plain HTML code in the PHP segment in the beginning.

Also, I believe you want

if ($value == $productName){


Related Topics



Leave a reply



Submit