0

In Yii2 I have integrated Kartik Depdrop widget. When I am using the Indian state and city in the table, it is working fine. but once I update the same table with US state and city,

the dependent field is not filled up and in firebug response is shown correctly but along with error - headers already sent exception like:

An Error occurred while handling another error: yii\web\HeadersAlreadySentException: Headers already sent in /var/www/clients/client2/web206/web/controllers/UserController.php on line 159. in /var/www/clients/client2/web206/web/vendor/yiisoft/yii2/web/Response.php:366

The relevant code is like this:

public function actionSubcat() {
$out = [];
if (isset($_POST['depdrop_parents'])) {
$parents = $_POST['depdrop_parents'];
if ($parents != null) {
$cat_id = $parents[0];
$out = UserProfile::GetCity($cat_id);
// the getSubCatList function will query the database based on the
// cat_id and return an array like below:
// [
// ['id'=>'<sub-cat-id-1>', 'name'=>'<sub-cat-name1>'],
// ['id'=>'<sub-cat_id_2>', 'name'=>'<sub-cat-name2>']
// ]
echo Json::encode(['output'=>$out,'selected'=>'']); // this is line 159
    return;
 }
         }
         echo Json::encode(['output'=>'','selected'=>'']);

    }

So I am not able to make, what is causing the issue and how I can fix it.

only difference to me it looks like is the number of database entries are more compared to Indian state and city.

Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68
Joshi
  • 2,730
  • 5
  • 36
  • 62
  • 1
    Why are you generating output in a _controller_? Sounds like someone needs to go read up on some MVC basics first of all. – CBroe Mar 22 '18 at 13:14
  • @CBroe - you may be quite right, but this is the extension by Kartik, who has so many extensions for Yii2. and for this extension the guide clearly instructs like that. I admit I am not an expert in Yii2, but assuming someone have host of extension and being used by the community, I assume that it is the correct way of doing it. - http://demos.krajee.com/widget-details/depdrop – Joshi Mar 22 '18 at 13:20
  • @CBroe - can you please suggest how I can fix that? – Joshi Mar 22 '18 at 13:21
  • Possible duplicate of [An Error occurred while handling another error: yii\web\HeadersAlreadySentException](https://stackoverflow.com/questions/49689315/an-error-occurred-while-handling-another-error-yii-web-headersalreadysentexcept) – rob006 May 27 '19 at 08:20

1 Answers1

2
echo Json::encode(['output'=>$out,'selected'=>'']); // this is line 159
return;

ALTER FOR

return Json::encode(['output'=>$out,'selected'=>'']);
Sahan Serasinghe
  • 1,591
  • 20
  • 33
Fak
  • 46
  • 3