I’m having issue on displaying data from the database under my populated dropdown lists.
Here is my codes.
For the Controller, i created an Action with a variable that will hold all the values from the database.
public function actionPollUnit($id)
{
$countpollunit = PollingUnit::find()->where(['uniqueid' => $id])->count();
$pollingunit= PollingUnit::find()->where(['uniqueid' => $id])->one();
#var_dump($pollingunit->pollingunitresults);
#die();
$output = '';
if($countpollunit > 0){
foreach($pollingunit->pollingunitresults as $pollingunitresult){
$output .= "<p value='".$pollingunitresult->polling_unit_uniqueid."'>".$pollingunitresult->party_abbreviation." - ".$pollingunitresult->party_score."</p>";
}
return $output;
}else{
return "no data";
}
}
Here is my view.
after the last Dropdown list i wanted the results from the database to display directly under.
<?= $form->field($model, 'pollingUnit')->dropdownlist(
ArrayHelper::map(PollingUnit::find()->all(), 'uniqueid', 'polling_unit_name'),
[
'prompt'=> 'select Polling Unit',
'onchange'=>'
$( "#output" ).html( `<div class="loader"></div>` );
$.post( "index.php?=election/pollunit?id='.'"+$(this).val(), function( data ){
if (data === "" || data === null){
$( "#output" ).html( "no data" );
}else{
$( "#output" ).html( data );
}
console.log(data);
});'
]); ?>
<div id="output" class="form-group">
</div>
<?php ActiveForm::end(); ?>