-1

I'm using yii2 and a newbie in JavaScript and ajax.

I am getting this error:

jquery.js:9175 POST http://localhost/istanagroup/web/purchase-order/basepricenet 500 (Internal Server Error)

here is the controller function

public function actionBasepricenet(){
$id = $_POST['id'];
$model = MasterItem::find()->andFilterWhere(['id_master_item'=>$id])->one();
echo $model;

here is the js :

$('#purchaseorderdetail-0-id_master_item').change(function(){
  var cat = $(this).val();
  $.ajax({
    type:'POST',
    url:'http://localhost/istanagroup/web/purchase-order/basepricenet',
    data : {id:cat},
    dataType : 'text',
    success:function(res){
      console.log(res);
      $('#purchaseorderdetail-0-base_price').val(res);
    }
  });
});
Jason Aller
  • 3,541
  • 28
  • 38
  • 38

1 Answers1

0

Folowing the Yii2 documentation,

andFilterWhere adds an additional WHERE condition to the existing one but ignores empty operands.

In your case you do not have declared a where() condition. Furthermore, once the ID is always provided in the request, you could change the andFilterWhere() to a normal where(), as the following:

public function actionBasepricenet(){
  if (isset($_POST['id'])){
    $id = $_POST['id'];
    $model = MasterItem::find()->where(['id_master_item'=>$id])->one();
    var_dump($model);
  } else {
     // ID not received! Error!
  }
}
Claudio Busatto
  • 721
  • 7
  • 17