How to get selected data from dropdown button retrieve from database? - php

Hye,
I want to get dropdown selected data.
For your information, my dropdown data is from my database. I already retrieve data from database and put it inside my dropdown.
Right now, I want to make validation. If user key in all information and forgot to key in 1 column data, the dropdown part should get the previous selected data right? So here are my coding, I still can get the previous selected data.
<div>
<x-label for="pizzatype" :value="__('Choose pizza type:')" />
<select name="pizzatype" id="pizzatype">
<option selected disabled>Please choose</option>
#foreach ($pizzainfo as $pizzaitem)
<option value="{{ old('$pizzaitem->name') }}">{{ $pizzaitem->name }}</option>
#endforeach
</select>
</div>
All dropdown button should get the selected previous button...

As I understand the problem is that you are trying to get an old() value that does not exist...
the quick fix is :
<select name="pizzatype" id="pizzatype">
<option #if(!old('pizzatype')) selected #endif disabled>Please choose</option>
#foreach ($pizzainfo as $pizzaitem)
<option #if(old('pizzatype') == $pizzaitem->name) selected #endif value="{{ $pizzaitem->name }}">{{ $pizzaitem->name }}</option>
#endforeach
</select>

Related

Laravel Blade Multiple Select Options Selected From Array

I have a select input that allows for multiple options to be selected. Because of the simple requirements, these selections are just stored as an array in my MySQL field. So for example they are stored as:
[Retail, Wholesale]
Now my question is, if I wanted the user to be able to edit the specific record, how would I pull the options up as selected in the select input?
The select field is as such:
<select class="form-control" name="type_industry">
<option value="Retail">Retail</option>
<option value="Wholesale">Wholesale</option>
<option value="Service">Service</option>
</select>
This is in a Laravel blade template, but if necessary I'm willing work with pure php answers or even javascript, I just need to be able to move past this issue.
Thanks!
You can use in_array() function,
<select class="form-control" name="type_industry">
<option value="Retail" #if(in_array("Retail", $selected_items_array)) selected #endif>Retail</option>
<option value="Wholesale" #if(in_array("Wholesale", $selected_items_array)) selected #endif>Wholesale</option>
<option value="Service" #if(in_array("Service", $selected_items_array)) selected #endif>Service</option>
</select>
if [Retail, Wholesale] is a php array ($savedOptions), and you have a list of all available options ($availableOptions) as another array, pass them to the blade view and in your blade just loop through:
#foreach ( $savedOptions as $value )
<option {{ in_array($value,$availableOptions) ? 'selected' : '' }} value={{ $value }}>{{ $value }}</option>
#endforeach

Laravel 6: Dynamic selectbox/droplist population from the database

As stated in the title, I want to dynamically change the available options of an HTML select box, based on the selection of another HTML select box, using PHP only if possible.
So far, I have managed to pass the data from each table into the view but I do not know how to use the primary and secondary keys in this context and without writing logic into the view file, respecting safety precautions.
What I have:
Controller:
...
use Illuminate\Support\Facades\DB;
...
public function create()
{
$sport_categories = DB::table('sport_categories')->get();
$sports = DB::table('sports')->get();
return view('events.create',
[
'sport_categories' => $sport_categories,
'sports' => $sports
]
);
}
View:
<div id="event-sport-category">
<span>Sport Category:</span>
<select name="sport-category">
<option value="default" selected="selected" disabled hidden>Select Sport Category</option>
#foreach($sport_categories as $sport_category)
<option value="{{ $sport_category->id }}" title="{{ $sport_category->desc }}">{{ $sport_category->name }}</option>
#endforeach
</select>
</div>
and
<div id="event-sport">
<span>Sport:</span>
<select name="sport">
<option value="default" selected="selected" disabled hidden>Select Sport</option>
#foreach($sports as $sport)
<option value="{{ $sport->id }}" title="{{ $sport->desc }}">{{ $sport->name }}</option>
#endforeach
</select>
</div>
The database tables are:
sport_categories (id, name, desc)
and
sports (id, sport_category_id, name, desc)
You'll need to use some sort of Javascript for this to work like you want. As #Clint suggested, since PHP is server-side, you won't be able to change your UI at runtime.
The good news is, you can keep everything you've already done. Depending on your comfort level with Javascript, it could be as easy as doing a show/hide of different html inputs based on what is selected.
Using jQuery, you could do something like this:
<div id="event-sport-category">
<span>Sport Category:</span>
<select name="sport-category" id="sportCategory">
<option value="default" selected="selected" disabled hidden>Select Sport Category</option>
#foreach($sport_categories as $sport_category)
<option value="{{ $sport_category->id }}" title="{{ $sport_category->desc }}">{{ $sport_category->name }}</option>
#endforeach
</select>
</div>
<div id="event-sport-one">
<span>Sport:</span>
<select name="sport" id="sport">
<option value="default" selected="selected" disabled hidden>Select Sport</option>
#foreach($sports as $sport)
<option value="{{ $sport->id }}" title="{{ $sport->desc }}">{{ $sport->name }}</option>
#endforeach
</select>
</div>
<div id="event-sport-two">
<span>Sport:</span>
<select name="sport" id="sportTwo">
<option value="default" selected="selected" disabled hidden>Select Sport</option>
#foreach($sportsTwo as $sportTwo)
<option value="{{ $sportTwo->id }}" title="{{ $sportTwo->desc }}">{{ $sportTwo->name }}</option>
#endforeach
</select>
</div>
jQuery
$(document).ready(function(){
$("#sportCategory").change(function(){
var category = $(this).children("option:selected").val();
console.log('selected category: ', category);
if (category == 'foo') {
// show sports related to foo
// hide & reset everything else
...
$('#event-sport-two').show();
$('#event-sport-one').hide();
}
});
});
CSS
#event-sport-two {
display: none;
}
Doing something like that would render/hide all of your available options when the page loads. You'd use CSS to hide the actual form elements, then using jQuery you could show/hide things as needed. This could get messy if you have a lot of things to manage.
Another option would be to make your form a Vue component. You could then use v-show based on what was chosen.
A final alternative (using either way) would be to listen for the input to change -- then make an ajax request to get your data and show the input based on the server's response.
Vue would give you a bit cleaner code. You could get the data and only show what's needed and not have to worry about all the show/hide business. :)
Hope that helps!

How to get id of selected value from dropdown and use it in other dropdown in laravel?

I'm newbie at Laravel and on stackoverflow. I'm making an app for registration. I want to show all those ids of section where class_id is selected in upper dropdown.
$cid=DB::table('classses')->get();
$counterr=0;
$sec=DB::table('sections')->where('class_id',$cid)->get();
$counterrr=0;
Dropdown menu for Class
<div class="col-md-6">
<select class="" name="class_id">
<option value="null">Class</option>
#foreach($cid as $cc)
#if($counterr==0)
<option selected="selected" value="{{$cc->id}}">{{$cc->title}}</option>
{$counterr++}}
#else
<option value="{{$cc->id}}">{{$cc->title}}</option>
#endif
#endforeach
</select>
Dropdown menu for Section where I want to get all the values of section where class_id is upper selected
<select section="" name="section_id">
<option value="null">Section</option>
#foreach($sec as $sc)
#if($counterrr==0)
<option selected="selected" value="{{$sc->id}}">{{$sc->title}}</option>
{{$counterrr++}}
#else
<option value="{{$sc->id}}">{{$sc->title}}</option>
#endif
#endforeach
</select>
Please help me in these case. You can freely ask anything if you want to.
You do not need to set a $counter in order to set the first select option to selected. Instead, you can check if the $key == 0:
<select class="" name="class_id">
<option value="null">Class</option>
#foreach($cid as $key => $cc)
<option {{ $key == 0 ? 'selected="selected"' : '' }} value="{{$cc->id}}">{{$cc->title}}</option>
#endforeach
</select>
Here we use a ternary expression to echo out the selected option based on if $key equals 0. If not, it will not echo out anything.

How to populate old select option values?

I have a select box in my form that looks like below :
<select class="form-control" name="category_id" id="category_id">
#if(count($category_list)>0)
<option value="null">Pick a Category</option>
#foreach($category_list as $cl)
<option value="{{$cl->id}}">{{$cl->name}}</option>
#endforeach
#else
<option>No categories found</option>
#endif
</select>
My question is, how do I populate the select option with old selected option when the validation fails ? By populate I mean like {{old('input')}}..
I tried this way :
#if(old('category_id')
<option value="{{old('category_id')}}">{{old name ???}}</option>
#endif
But it will only get the category_id value, not the category name. How to get the old category name as well ? that's all and thanks!
you can do like this
#if(count($category_list)>0)
<option value="null">Pick a Category</option>
#foreach($category_list as $cl)
<option value="{{$cl->id}}" #if(old('category_id') == $cl->id) selected="selected" #endif>{{$cl->name}}</option>
#endforeach
#else
<option>No categories found</option>
#endif

Laravel select menu to show selected from DB

How can i get a dropdown to select whatever id is present within the database.
I have a user's table that has a location_id and a towns table that has town_id and town_name so I want the dropdown to know what the user table location_id is and show the town_name based on the id.
How can this be done in Laravel?
Thanks in advance.
What you would do is assign your user and towns to the view. Next loop your towns table to generate your select list. Something like:
<select name="location">
#foreach($towns as $town)
<option value="{{ $town->id }}" {{ ($user->location_id == $town_id) ? "selected" : "" }}>{{ $town->town_name }}</option>
#endforeach
</select>
Ok fixed it to match blade:
<select name="location" class="form-control">
# foreach($towns as $town)
<option value="{{ $town->town_id}}" #if ($user->location == $town->town_id) "selected" #else "" #endif>{{ $town->town }}</option>
#endforeach
</select>
But it's not showing the selected item
If you are sending correct data to the view and the problem is only repopulating the select then you may try this, just put this in your view and send $towns from your controller as you are sending now
{{ Form::select('towns', $towns, Input::old('towns')) }}
No need to manually loop through the array. Laravel's Form class will build the select/dropdown.
My last answer works, just find it was cached by the looks in the browser, so all fixed.

Categories