getting selected value from drop down list in laravel - php

I'm new to laravel, in the view page I'm showing a Dropdown-List which is fetched from the database and based on that Dropdown-List value selection, I'm getting the corresponding value id. But I also want one more corresponding fields from database based on the Dropdown-List I'm selecting.
How to get it? Based on that values I want to do Javascript operation.
Here's my code:
{{
Form::select('asset_type_id',$assettype_list, Input::old('asset_type_id'),
array('class'=>'select2', 'onchange' => 'check(Input::get(this.value);)',
'style'=>'width:350px'))
}}

You cannot use Input::get(this.value) in your onchange as it will be printed literally. You can just use the code without the Input::get. Of course, this all depends on how you are using the value in your javascript.
{{
Form::select('asset_type_id',$assettype_list, Input::old('asset_type_id'),
array('class'=>'select2', 'onchange' => 'check(this.value)',
'style'=>'width:350px'))
}}

Related

Laravel: set checkboxes as default set

I have one really simple question. I have a form with three groups of dynamically generated check boxes. Let's say one of these groups of check boxes needs to have all check boxes as default set.
This is not a problem, the problem comes when I submit the form and I need to have the state of the checkbox.
Here is the HTML code I generate:
#for($i=0;$i<24;++$i)
{!! Form::checkbox('check_hour[]', $i, $check_hour[$i], ['class' => 'check_hour']) !!} {{($i+1)}}h
#endfor
and here is the code in the Controller:
for($i=0;$i<24;++$i) $check_hour[$i] = (isset(Input::get('check_hour')[$i])) ? true : false;
In this situation I have no problem to save the checkbox state, but I need all check boxes to bet set as default.

Php Laravel bind php variable with html select

I am very new to php laravel.
I need to populate a dropdown in php blade with certain list I send to the view and I need to bind the selected value with a certain php variable.
My code:
<div class="dropdown" id="managerDropdown">
{{ Form::select($row->manager_id,Manager::getSelectOptions(),$row->manager_id) }}
</div>
I'm not understanding how do I bind my "$row->manager_id" with this drop down, so that when I submit, "$row->manager->id" will have selected records id(value).

Laravel 4 Blade Form Formatting (Hidden Input)

I have an "artist" resource, and on the show route page I have a form. I am using Laravel Blade syntax. As part of this form, I am trying to send the page ID to the back end of a Laravel 4 project. I am doing it this way:
{{Form::hidden('artist-id',null, array(
'id' => '{{$artist->id}}',
));}}
However, when using this, the browser throws an error, because it is not rendering the $artist->id variable within the brackets before the outside brackets of the input form.
Is there a way around this or another way to pass back this variable for the resource? Thank you!
So anything inside the blade tags {{ }} is treated as standard PHP. Using further {{ }} inside existing blade tags is just going to throw a big error.
Because anything inside the blade tags is treated as PHP you can simply do the following
{{ Form::hidden('artist-id', null, ['id' => $artist->id]) }}
Although posting that input isn't going to give you the value you want, because the value supplied is null. The id attribute is the html ID given to the html attribute. You need the following to set the value on the input, which will then be posted in with your form data
{{ Form::hidden('artist-id', $artist->id) }}
You don't need to nest your blade call like that, this should work:
{{ Form::hidden('artist-id', null, array('id' => $artist->id)) }}

Laravel - set checkbox / radio selected in edit mode

In Laravel, we can set the value for a form field element with this:
{{Form::text('name', 'predefined name')}}
The above work well, even in the edit mode the predefined name will always appear in the text field. However, for checkbox and radio button, it does not work.
{{Form::checkbox('country', 'UK',true)}}
{{Form::radio('gender', 'male',true)}}
{{Form::radio('gender', 'female',false)}}
No matter what I set for the checkbox & radio button, it will be ignored and use the data from database. This become an huge trouble for array of checkbox.
{{Form::checkbox('country[]', 'UK',true)}}
{{Form::checkbox('country[]', 'SG',false)}}
{{Form::checkbox('country[]', 'US',false)}}
How can I solve it? Thank you.
If you are referring to the visibility of the value of a checkbox or radio-array:
Usually you will add text to a checkbox or radio-element via a label.
{{ Label::for('nameOfInput', 'Value') }}

array filed values not working in Laravel blade template system?

I have 2 email fields and I am using
{{ Form::text('email[]', Input::old('email'),array('class' => 'large-2', 'placeholder' => 'email address','id'=>'email')) }}`
if I use [] to get multiple values for same variable, it is giving error in view page if posted back some data. for example if some fields are mandatory and if user fail to fill those, page will be redirected to same view page from where it was launched.
In such cases it is showing error.
How to fix this issue?
One text field can contain only one data. So, you need another text field to achieve that. if you don't want to show the multiple emails, you can use hidden fields.
{{ Form::hidden('email[]', Input::old('email1'))
{{ Form::hidden('email[]', Input::old('email2'))
Or you can use select. Laravel allow array data if use select field.
For example:
{{ Form::select('size', array('L' => 'Large', 'S' => 'Small')) }}
http://laravel.com/docs/html#drop-down-lists

Categories