Reputation: 89
i am having trouble trying to pass two variables in an onchange in yii2. The below code doesn't work.
_form
<?= $form->field($model, 'commodity')->dropDownList(
ArrayHelper::map(['empty'=>'Empty string'], 'id', 'value'),
[
'prompt'=>'------- Select --------',
'id'=>'pcommodity',
'disabled'=>"disabled",
'style' => 'width:250px',
'onchange'=>
'$.post("/import-conditions/plants/listsspecies?
name='.'" +$(this).val(),function(data)
{
$( "select#pspecies").html(data)
})
here is the problem
**$.post("/import-conditions/plants/listsintendeduse?name='.'"
+$(this).val(),function(data)'.'+$(category).val(),function(data)
{
$( "select#pintendeduse").html(data)
})**
$.post("/import-conditions/plants/listsorigin?name='.'"
+$(this).val(),function(data)
{
$( "select#porigin").html(data)
})
;'
])->label(false);?>
This form information is passed to the controller here is the code for the controller.
public function actionListsintendeduse($name,$category)
{
$countMaindata= Plants::find()
->where(['commodity'=> $name,'category'=>$category])
->count();
$maindata = Plants::find()
->select('intendeduse')
->where(['commodity'=> $name,'category'=>$category])
->orderBy(['intendeduse'=>SORT_ASC])
->distinct()
->all();
if($countMaindata > 0)
{
// echo '<option value="">Select intendeduse </option>';
foreach ($maindata as $main)
{
echo "<option value='".$main->intendeduse."'> ".$main->intendeduse."</option>";
}
}else{
echo "<option> - </option>";
}
}
I am trying to pass two variables and have the controllers filter by those two variables.
Upvotes: 1
Views: 309
Reputation: 218
You have a syntax error
try this
$.post(
"/import-conditions/plants/listsintendeduse?name="
+$(this).val() + "&category=" + $(category).val(),
function(data) {
$( "select#pintendeduse").html(data)
}
)
Upvotes: 2