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
Reading Numbers as Text Format With Phpexcel
How to Fix Undefined Offset: 0 in Laravel Blade
Sql Get an Id from a Column Where Ids Separated by Commas
Failed to Open the System Default Web Browser. Is One Installed - Pgadmin4
On a Function That Gets Settings from a Db I Ran into the Error
What Is the Regular Expression to Validate a Comma Delimited List But Ending With '&' and a Word
Comparing Two Dates in a SQL Request
Why Shouldn't I Use MySQL_* Functions in PHP
How to Implement Basic "Long Polling"
Replace Preg_Replace() E Modifier With Preg_Replace_Callback
How to Get the Sucess Message in the Same Page After Submitting the Contact Form
How to Prevent a User from Directly Accessing My HTML Page by Writing Url
Regex for Only Allowing Letters, Numbers, Space, Commas, Periods
Laravel - Page Not Found Even That the Route Exists
Undefined Index: User_Id. Where, User_Id Is Session Variable