<?php
$url = Yii::$app->urlManager->createUrl(['admin/sale/prduct']);
?>
script code in view page send id whit GET
<script>
function ddlcategor(id){
$.ajax({
type:'GET',
url:'<?=$url?>',
data:{id:id},
success: function(data){
$("#test").html(data);
}
});
}
</script>
controller document !
<?php
public function actionProduct($id){
$products = Yii::db->createCommand('select products.* from products right join (select * from product_category where product_category.cat_pro_id ='.$id.') as t on(products.id = t.product_id)')->queryAll();
$option ='';
echo "<option>select ...</option>";
foreach($products as $value){
$option.="<option value=$value->id>$value->title</option>";
}
return $option;
}
?>
PHP Notice - yii\base\ErrorException typing to get property of non-object
Yii::$app->db->createCommand()
returns array. Each row is an associative array with column names and values.
if the selection returns nothing, an empty array will be received.
Yii::$app->db->createCommand()->queryAll();
In your example $value not objact. It is array:
$products = Yii::db->createCommand('select products.* from products right join (select * from product_category where product_category.cat_pro_id ='.$id.') as t on(products.id = t.product_id)')->queryAll();
$option ='';
//No needed in this variant
//echo "<option>select ...</option>";
If(!empty($products)){
foreach($products as $value){
$option.="<option value=$value['id']>$value['title']</option>";
}
}else{
$option.= "<option selected disabled>No results!</option>"
}
return $option;
To debug ajax result I recommend using https://www.getpostman.com/ Using this service, you can track results and errors returned by url pasted to ajax simply.