yii2- how to get drop down selected value from view to controller - yii2

I am working on yii2. In my view, I have a button which will perform downloading of pdf file. Also, I have drop downs and I want to pass the selected drop-down value from my view to the controller. The drop-down in my index view is.
[
'label' => 'Sub Division Name',
'value' => function ($d) {
if(is_object($d->subdiv))
return $d->subdiv->name;
return ' - ';
},
'filter' => Html::activeDropDownList($searchModel, 'sub_div', \common\models\SurveyHescoSubdivision::toArrayList(), ['prompt' => "Sub-Div", 'class' => 'form-control']),
],
The HTML generated is
<select id="meterssearch-sub_div" class="form-control" name="MetersSearch[sub_div]">
<option value="">Sub-Div</option>
<option value="37111" selected="">37111 - HYD SADDAR</option>
<option value="37112">37112 - GARI KHATA</option>
<option value="37113">37113 - SARFARAZ COLONY</option>
<option value="37115">37115 - LIAQAT COLONY</option>
<option value="37141">37141 - TANDO ALLAH YAR-I</option>
<option value="37142">37142 - TANDO ALLAH YAR-II</option>
<option value="37143">37143 - TANDO JAM</option>
<option value="37144">37144 - CHAMBER</option>
<option value="37151">37151 - HALI ROAD</option>
<option value="37152">37152 - RIZVI HOSPITAL</option>
<option value="37153">37153 - ALLAMA IQBAL</option>
<option value="37154">37154 - SH:UMAID ALI KHAN</option>
<option value="37155">37155 - MIRAN M.SHAH</option>
<option value="37181">37181 - QASIMABAD</option>
<option value="37182">37182 - CITIZEN COLONY</option>
<option value="37183">37183 - HIRABAD</option>
<option value="37221">37221 - KOTRI</option>
<option value="37222">37222 - JAMSHORO</option>
<option value="37223">37223 - SHAHBAZ</option>
<option value="37225">37225 - NOORIABAD</option>
<option value="37226">37226 - SEHWAN SHARIF</option>
<option value="37241">37241 - PARETABAD</option>
<option value="37242">37242 - PHULELI</option>
<option value="37243">37243 - ILYASABAD</option>
<option value="37244">37244 - MEMON HOSPITAL</option>
<option value="37251">37251 - T.MUHAMMAD KHAN-I</option>
<option value="37252">37252 - T.MUHAMMAD KHAN-II</option>
<option value="37253">37253 - MATLI</option>
<option value="37254">37254 - BULRISHAH KARIM</option>
<option value="37271">37271 - THATTA</option>
<option value="37272">37272 - SUJAWAL</option>
<option value="37273">37273 - MAKLI</option>
<option value="37281">37281 - BADIN</option>
<option value="37282">37282 - TALHAR</option>
<option value="37283">37283 - GOLARCHI</option>
<option value="37311">37311 - NAWAB SHAH-I</option>
<option value="37312">37312 - NAWAB SHAH-II</option>
<option value="37313">37313 - SOCIETY</option>
<option value="37314">37314 - DAUR</option>
<option value="37315">37315 - SAKRAND</option>
<option value="37316">37316 - SAEEDABAD</option>
<option value="37318">37318 - QAZI AHMED</option>
<option value="37319">37319 - DAULAT PUR</option>
<option value="37331">37331 - TANDO ADAM-I</option>
<option value="37332">37332 - TANDO ADAM-II</option>
<option value="37333">37333 - ODERO LAL</option>
<option value="37334">37334 - MATIARI</option>
<option value="37335">37335 - HALA</option>
<option value="37336">37336 - BHIT SHAH</option>
<option value="37341">37341 - SANGHAR</option>
<option value="37342">37342 - JHOLE</option>
<option value="37343">37343 - SHAHDAD PUR-I</option>
<option value="37344">37344 - SHAHDAD PUR-II</option>
<option value="37345">37345 - KHIPRO</option>
<option value="37346">37346 - SHAHPUR CHAKAR</option>
<option value="37347">37347 - SINDHRI</option>
<option value="37411">37411 - MIPURKHAS CITY</option>
<option value="37412">37412 - SATELLITE TOWN</option>
<option value="37413">37413 - HIRABAD MPK</option>
<option value="37414">37414 - MIRWAH</option>
<option value="37421">37421 - DIGRI</option>
<option value="37422">37422 - JHUDO</option>
<option value="37423">37423 - NAUKOT</option>
<option value="37424">37424 - MITHI</option>
<option value="37431">37431 - UMERKOT</option>
<option value="37432">37432 - PITHORO</option>
<option value="37433">37433 - SAMARO</option>
<option value="37434">37434 - KUNRI</option>
</select>
Update 1
My index view code is
Two buttons
<p>
<?= Html::a('<i></i> Create Meters', ['create'], ['class' => 'btn btn-success']) ?>
Download PDF// on this button click the actionReport() is called
</p>
GridView
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'description',
'meter_msn',
//'meter_type',
[
'label' => 'Meter Type',
'value' => function ($d) {
if(is_object($d))
return $d->meter_type;
return ' - ';
},
'filter' => Html::activeDropDownList($searchModel, 'meter_type', \app\models\Meters::getMeterTypeValues(), ['prompt' => "Meter Type", 'class' => 'form-control']),
],
//'sub_div',
[
'label' => 'Sub Division Name',
'value' => function ($d) {
if(is_object($d->subdiv))
return $d->subdiv->name;
return ' - ';
},
'filter' => Html::activeDropDownList($searchModel, 'sub_div', \common\models\SurveyHescoSubdivision::toArrayList(), ['prompt' => "Sub-Div", 'class' => 'form-control']),
],
//'meter_status',
[
'label' => 'Meter Status',
'value' => function ($d) {
return $d->meter_status;
},
'filter' => Html::activeDropDownList($searchModel, 'meter_status', \app\models\Meters::getMeterStatus(), ['prompt' => "Meter Status", 'class' => 'form-control']),
],
//'status',
[
'label'=>'Status',
'value'=>function($a){
return $a->getStatusvalue();
}
],
[
'label' => 'Issued User',
'value' => function ($d) {
if(is_object($d->user))
//return $d->user->name;
return $d->issued_user == '' ? '' : $d->user->username;
return ' - ';
// return $d->user->name;
},
'filter' => Html::activeDropDownList($searchModel, 'issued_user', \app\models\User::toArrayList(), ['prompt' => "Users", 'class' => 'form-control']),
],
[
'label' => 'Created By',
'value' => function ($data) {
if (is_object($data))
return $data->created->name;
return ' - ';
},
],
'created_at:date',
'historic',
'comments',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
On button click, an action controller is called and inside it, I am trying to access the value of the select sub_div.
function actionReport()
{
$sub_div = isset($_REQUEST['MetersSearch[sub_div]'])?$_REQUEST['MetersSearch[sub_div]']:'';
print_r($sub_div);
die();
}
But it's giving me empty view with no value shown.
How can I access it? Any help would be highly appreciated.

You need filter value in Report controller.For that you need to pass values using Querystring.
you can get filter value in view index page as below.
$sub_div = isset($_REQUEST['MetersSearch']['sub_div'])?$_REQUEST['MetersSearch']['sub_div']:'';
Now to can pass this values to controller.
Download PDF
In report controller you will get sub_div
function actionReport()
{
$sub_div = $_REQUEST['sub_div'];
print_r($sub_div);
die();
}

Related

Select Options: replace text with new value

I feel as though I am very close to a solution for this. I searched for similar questions but I do not believe I have found the answer I am looking for.
What I would like:
I have a select dropdown with long text options, and would like to replace the value from handle change with the first word in the option, and set that as the new button select option.
What I currently have:
I modify the incoming handleChange to the first word, attempt to set it on the select button, and it never happens.
codepen: https://codepen.io/anon/pen/gdXVJM?editors=1011
code:
HTML
<div id="input"></div>
JS
class MyInput extends React.Component{
constructor(props){
super(props);
this.state = {
buttonStyle: '',
voiceSelected: ''
}
this.handleChange = this.handleChange.bind(this);
}
handleChange(event) {
event.persist();
if (event.target.id === 'voiceSelected') {
//set the state we're going to use later
this.setState(() => ({ 'voiceSelected': event.target.value}));
//Take the first word from the string to make it visable on button
let totalWords = event.target.value
var firstWord = totalWords.replace(/ .*/,'');
console.log(firstWord)
this.setState(() => ({'buttonStyle': firstWord}));
} else {
console.log("Something Went Wrong!")
}
}
render(){
return <select
id="voiceSelected"
value={this.state.buttonStyle}
name={this.state.buttonStyle}
onChange={(e) => {this.handleChange(e)}}
>
<option value="" selected disabled hidden>Choose Voice</option>
<option value="Ivy">Ivy [English - American]</option>
<option value="Joanna">Joanna [English - American]</option>
<option value="Joey">Joey [English - American]</option>
<option value="Justin">Justin [English - American]</option>
<option value="Kendra">Kendra [English - American]</option>
<option value="Kimberly">Kimberly [English - American]</option>
<option value="Salli">Salli [English - American]</option>
<option value="Nicole">Nicole [English - Australian]</option>
<option value="Russell">Russell [English - Australian]</option>
<option value="Emma">Emma [English - British]</option>
<option value="Brian">Brian [English - British]</option>
<option value="Amy">Amy [English - British]</option>
<option value="Raveena">Raveena [English - Indian]</option>
<option value="Geraint">Geraint [English - Welsh]</option>
<option value="Ricardo">Ricardo [Brazilian Portuguese]</option>
<option value="Vitoria">Vitoria [Brazilian Portuguese]</option>
<option value="Lotte">Lotte [Dutch]</option>
<option value="Ruben">Ruben [Dutch]</option>
<option value="Mathieu">Mathieu [French]</option>
<option value="Celine">Celine [French]</option>
<option value="Chantal">Chantal [Canadian French]</option>
<option value="Marlene">Marlene [German]</option>
<option value="Dora">Dora [Icelandic]</option>
<option value="Karl">Karl [Icelandic]</option>
<option value="Carla">Carla [Italian]</option>
<option value="Giorgio">Giorgio [Italian]</option>
<option value="Mizuki">Mizuki [Japanese]</option>
<option value="Liv">Liv [Norwegian]</option>
<option value="Maja">Maja [Polish]</option>
<option value="Jan">Jan [Polish]</option>
<option value="Ewa">Ewa [Polish]</option>
<option value="Cristiano">Cristiano [Portuquese]</option>
<option value="Ines">Ines [Portuquese]</option>
<option value="Carmen">Carmen [Romanian]</option>
<option value="Maxim">Maxim [Russian]</option>
<option value="Tatyana">Tatyana [Russian]</option>
<option value="Enrique">Enrique [Spanish]</option>
<option value="Penelope">Penelope [US Spanish]</option>
<option value="Enrique">Miguel [US Spanish]</option>
<option value="Conchita">Conchita [Castilian Spanish]</option>
<option value="Astrid">Astrid [Swedish]</option>
<option value="Filiz">Filiz [Turkish]</option>
<option value="Gwyneth">Gwyneth [Welsh]</option>
</select>
}
}
ReactDOM.render(<MyInput/>, document.getElementById('input'));
<select> doesn't have 'value' attribute. we can get the value of select box by using 'selected' attribute in <options>
Please refer doc
I don't know if it suits you but if you can keep your options in the state you can somehow manipulate them. But with this method, you are losing the long values when you select an option. Maybe you can improve the logic starting with this answer.
class App extends React.Component {
state = {
options: [
"Ivy [English - American]",
"Joanna [English - American]",
"Joey [English - American]",
"Justin [English - American]"
],
selectedIndex: 0
};
handleChange = event => {
const { selectedIndex, value } = event.target;
const { options } = this.state;
var firstWord = value.replace(/ .*/, "");
const newOptions = Object.assign([], options, {
[selectedIndex - 1]: firstWord
});
this.setState({ options: newOptions, selectedIndex });
};
render() {
const { options } = this.state;
return (
<select
id="voiceSelected"
value={this.state.options[this.state.selectedIndex - 1]}
name={this.state.buttonStyle}
onChange={this.handleChange}
>
<option value="">Choose Voice</option>
{
options.map( option =>
<option key={option}>{option}</option>
)
}
</select>
);
}
}
ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"></div>

Keep the option selected in search form - Laravel

I have a search form that searches for responsible, date and device name. When someone searches for a resposible with a date, I want that search data to be maintained in the result
The entire search form
{{ Form::open(['route' => 'reviews.index', 'method' => 'GET', 'class' => 'form-inline pull-right']) }}
#csrf
{{ Form::text('device_name', null, ['class' => 'form-control', 'placeholder' => 'Nombre Equipo'])}}
<select name="name">
<option></option>
<option>OK</option>
<option>NOK</option>
</select>
{{ Form::date('created_at', null, ['class' => 'form-control', 'placeholder' => 'Creacion'])}}
<select name="responsable">
<option ></option>
#foreach($users as $user)
<option>{!! $user->name !!}</option>
#endforeach
</select>
<button style="border: none;padding: 4px 20px;background-color: #d61628;color: white">Buscar</button>
<button style="border: none;padding: 4px 20px;background-color: #d61628;color: white">Reset</button>
{{ Form::close() }}
And the index method in Controller
public function index(Request $request)
{
$responsable = $request->get('responsable');
$created_at = $request->get('created_at');
$device_name = $request->get('device_name');
$name = $request->get('name');
$devices = Device::all();
$reviews = Review::orderBy('id', 'DESC')
->responsable($responsable)
->created_at($created_at)
->device_name($device_name)
->name($name)
->paginate(30);
$users = User::all();
return view('reviews.index', compact('devices', 'reviews', 'users'));
}
Thanks
You may use old() function for re-populate the form with the last submitted data.
See https://laravel.com/docs/5.6/requests#old-input
So, in your blade code it would be something like this:
{{ Form::text('device_name', old('device_name'), ['class' => 'form-control', 'placeholder' => 'Nombre Equipo'])}}
//...
{{ Form::date('created_at', old('created_at'), ['class' => 'form-control', 'placeholder' => 'Creacion'])}}
For select, it's different, basically you compare each rendered option in the loop and if it's the one submitted you mark it as selected with html.
<select name="responsable">
<option></option>
#foreach($users as $user)
<option value="{{ $user->name }}" {{ old('responsable') == $user->name ? 'selected' : '' }}>{!! $user->name !!}</option>
#endforeach
</select>
Also, consider that old() also accepts a second parameter for default value in case you don't want it to be just null.

Auto increase and decrease height of multi-select multiple bootstrap select2

I use a multi select with bootstrap 3 and select2 where users can choose the months of the year. If I were to select all the months in the year as you can see in the image the selections take up more space than the input.
Is it possible to auto increase and decrease the input height to match the selections?
I tried adding the css inline on the select with height: auto but that didn't work. Even specifying a height did not work either.
HTML
<select class="form-control" id="scheduler_months" multiple style="width: 100%">
<option selected value='1'><?=_("Janaury")?></option>
<option value='2'><?=_("February")?></option>
<option value='3'><?=_("March")?></option>
<option value='4'><?=_("April")?></option>
<option value='5'><?=_("May")?></option>
<option value='6'><?=_("June")?></option>
<option value='7'><?=_("July")?></option>
<option value='8'><?=_("August")?></option>
<option value='9'><?=_("September")?></option>
<option value='10'><?=_("October")?></option>
<option value='11'><?=_("November")?></option>
<option value='12'><?=_("December")?></option>
</select>
UPDATE
Since I didn't realize that the select2 was creating the issue I found my problem here and simply used the following css to fix my issue.
.select2-selection--multiple{
overflow: hidden !important;
height: auto !important;
}
So now I get
Am using yii2 php web framework, Can you check this if it may Help, https://demos.krajee.com/widget-details/select2#google_vignette
<?php
echo Select2::widget([
'name' => 'field_name',
'data' => ['2'=>'February',
'3'=>'March', '4'=>'April', '5'=>'May', '6'=>'June'],
'theme' => Select2::THEME_MATERIAL,
'options' => ['placeholder' => 'Select a state ...', 'autocomplete' => 'off'],
'pluginOptions' => [
'allowClear' => true
],
]);
?>

CakePHP 3 - populate select dropdown with data from custom table

So I started looking at CakePHP, and I'm just doing a few test cases to see if CakePHP will cater for all my needs.
I have a custom table (Table name: roles):
id role sub_site
1 Role 1 0
2 Role 2 0
3 Role 3 0
4 Role 4 0
5 Role 1 1
In my controller (User controler) I have the following code based on all the feedback here.
$this->loadModel("roles");
$rolesList = $this->roles->find('list', array(
'conditions' => array('sub_site' => '0'),
'fields' => array('id', 'role')
));
$this->set(compact('rolesList'));
I have the following in the view
$this->Form->input('role', array('label'=>false,
'div'=>false,
'type'=>'select',
'empty'=>'-- select one --',
'options'=>$rolesList));
Yet the output looks like this:
<select name="role" required="required" id="role">
<option value="">-- select one --</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
All I want is the output as follows:
<select name="role" required="required" id="role">
<option value="">-- select one --</option>
<option value="1">Role 1</option>
<option value="2">Role 2</option>
<option value="3">Role 3</option>
<option value="4">Role 4</option>
</select>
Please indicate what is wrong here as this is suppose to be simple to just populate?
I am getting results using this method. In my controller:
$articles = TableRegistry::get('Transfers');
$make=$articles->find('list', ['keyField' => 'id','valueField' => 'make']);
$this->set('make',$make);
and then in view:
echo $this->Form->input('make',['type'=>'select','options'=>$make ,'empty'=>'-Select Make-','class'=>'form-control','templateVars'=>['class'=>'col-md-4']]);
Hope you will get results.

html multi <select> array in PHP post

<form>
<input class="date-pick" type="text" value="2010/08/02" name="date" />
<table id="events_selection">
<tr>
<td>
<img src="/admin/ajax/image.php?filename=/media/immagini/danieledaniela/1.jpg&maxw=200" />
<select name="1">
<option value="0"> NESSUN EVENTO </option>
<option value="5">anelli</option>
<option value="8">ballo</option>
<option value="6">cerimonia in chiesa</option>
<option value="13">dettagli</option>
<option value="9" selected="selected"><strong>festa</strong></option>
<option value="4">gli sposi</option>
<option value="1">la sposa</option>
<option value="3">lancio del riso</option>
<option value="2">lo sposo</option>
<option value="11">preparazione sposa</option>
<option value="10">preparazione sposo</option>
<option value="7">ristorante</option>
<option value="12">varie</option>
</select>
</td>
<td>
<img src="/admin/ajax/image.php?filename=/media/immagini/danieledaniela/30.jpg&maxw=200" />
<select name="31">
<option value="0"> NESSUN EVENTO </option>
<option value="5">anelli</option>
<option value="8">ballo</option>
<option value="6">cerimonia in chiesa</option>
<option value="13">dettagli</option>
<option value="9">festa</option>
<option value="4">gli sposi</option>
<option value="1">la sposa</option>
<option value="3">lancio del riso</option>
<option value="2">lo sposo</option>
<option value="11">preparazione sposa</option>
<option value="10">preparazione sposo</option>
<option value="7">ristorante</option>
<option value="12">varie</option>
</select>
</td>
</tr>
</table>
</form>​​​​​​​​
HI, in a situation like this (which is an example, there are many selects and other inputs) posting this form would give a $_POST like this
array {
date => "2010/08/09"
input1 => "bla"
input3 => "bla2"
//list of selection
1 => 2
2 => 4
3 => 5
//ends list of selection
input 4 => "bla4"
}
Can I directly from the HTML form put the <select>s in seperate array since they represent a grouped array?
array {
date => "2010/08/09"
input1 => "bla"
input3 => "bla2"
//list of selection
array {
1 => 2
2 => 4
3 => 5
}
//ends list of selection
input 4 => "bla4"
}
Thanks!
This:
<select name="selection[1]">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
<select name="selection[2]">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
Would return:
'selection' => array(1 => 1, 2 => 1)
Guess that's what you're looking for? :-)