<?php foreach ($price as $k) : ?>
<?php $form = ActiveForm::begin() ?>
<?= $form->field($k, 'category_id')->hiddenInput()->label(false) ?>
<tr>
<td>1</td>
<td><?= $k->category_id ?></td>
<td>
<div class="col-md-2">
<?= $form->field($k, 'price')->textInput()->label(false) ?>
</div>
</td>
<td><?= Html::submitButton('<span class="glyphicon glyphicon-ok"></span>', ['class' => 'btn btn-success']) ?></td>
</tr>
<?php $form = ActiveForm::end() ?>
<?php endforeach; ?>
<div class="col-lg-6">
<?php $form = ActiveForm::begin() ?>
<?= $form->field($profileHasRegioncity, 'region_id')->dropDownList($region,
[
'prompt' => 'Выбрать область',
'onchange' => '
$.post(
"' . Url::toRoute('/user/ajax') . '",
{id: $(this).val()},
function(data){
$("select#city_id").html(data).attr("disabled", false)
})'
]
) ?>
</div>
<div class="col-lg-6">
<?= $form->field($profileHasRegioncity, 'city_id')->dropDownList($city,
[
'prompt' => 'Выбрать город',
'id' => 'city',
]
) ?>
</div>
<?php $form = ActiveForm::end() ?>
public function actionAjax()
{
if (Yii::$app->request->isAjax) {
$id = (int)Yii::$app->request->post('id');
$this->option = "<option value='0'>" . 'Выбрать область' . "</option>";
$cities = RegionCity::find()
->where('parent_id=:id', [':id' => $id])
->all();
foreach ($cities as $city) {
$this->option .= '<option value="' . $city->id . '">' . $city->name_ru . '</option>';
}
}
return $this->option;
}
2017-04-14 10:21:37 [127.0.0.1][-][-][error][yii\base\UnknownPropertyException] exception 'yii\base\UnknownPropertyException' with message 'Setting unknown property: app\controllers\UserController::option' in /var/www/html/aloha/vendor/yiisoft/yii2/base/Component.php:201
Stack trace:
#0 /var/www/html/aloha/controllers/UserController.php(294): yii\base\Component->__set('option', '<option value='...')
#1 [internal function]: app\controllers\UserController->actionAjax()
#2 /var/www/html/aloha/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#3 /var/www/html/aloha/vendor/yiisoft/yii2/base/Controller.php(156): yii\base\InlineAction->runWithParams(Array)
#4 /var/www/html/aloha/vendor/yiisoft/yii2/base/Module.php(523): yii\base\Controller->runAction('ajax', Array)
#5 /var/www/html/aloha/vendor/yiisoft/yii2/web/Application.php(102): yii\base\Module->runAction('user/ajax', Array)
#6 /var/www/html/aloha/vendor/yiisoft/yii2/base/Application.php(380): yii\web\Application->handleRequest(Object(yii\web\Request))
#7 /var/www/html/aloha/web/index.php(12): yii\base\Application->run()
#8 {main}
2017-04-14 10:21:37 [127.0.0.1][-][-][info][application] $_POST = [
'id' => '2'
]
$_COOKIE = [
'_csrf' => '2a3eaab79a982e6efba9086f7e733fa2a668fa0109173c43f89fe8bc5fc1564fa:2:{i:0;s:5:\"_csrf\";i:1;s:32:\"edYRYeo60aOIVoqZckGpgDT6tSg44mAt\";}'
'PHPSESSID' => 'i0fqvopnu98omqkgvulrquad40'
'_identity' => 'acc2d511cc66cb2d8a0468bf94c589a412aa9e5c639266c6bd25747924962cf8a:2:{i:0;s:9:\"_identity\";i:1;s:47:\"[16,\"r5TKm16fRfl7EbOOBf7TA3xPMcuzpxfg\",2592000]\";}'
]
$_SERVER = [
'REDIRECT_STATUS' => '200'
'HTTP_HOST' => 'aloha'
'HTTP_CONNECTION' => 'keep-alive'
'CONTENT_LENGTH' => '4'
'HTTP_ORIGIN' => 'http://aloha'
'HTTP_X_CSRF_TOKEN' => 'bkZHdDZFOC4LIh4mbyBXGF4nCD1gKkl0DS0ABFEBbBgaFSBAAih5Wg=='
'HTTP_USER_AGENT' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36'
'CONTENT_TYPE' => 'application/x-www-form-urlencoded; charset=UTF-8'
'HTTP_ACCEPT' => '*/*'
'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'
'HTTP_REFERER' => 'http://aloha/user/profile'
'HTTP_ACCEPT_ENCODING' => 'gzip, deflate'
'HTTP_ACCEPT_LANGUAGE' => 'uk-UA,uk;q=0.8,ru;q=0.6,en-US;q=0.4,en;q=0.2'
'HTTP_COOKIE' => '_csrf=2a3eaab79a982e6efba9086f7e733fa2a668fa0109173c43f89fe8bc5fc1564fa%3A2%3A%7Bi%3A0%3Bs%3A5%3A%22_csrf%22%3Bi%3A1%3Bs%3A32%3A%22edYRYeo60aOIVoqZckGpgDT6tSg44mAt%22%3B%7D; PHPSESSID=i0fqvopnu98omqkgvulrquad40; _identity=acc2d511cc66cb2d8a0468bf94c589a412aa9e5c639266c6bd25747924962cf8a%3A2%3A%7Bi%3A0%3Bs%3A9%3A%22_identity%22%3Bi%3A1%3Bs%3A47%3A%22%5B16%2C%22r5TKm16fRfl7EbOOBf7TA3xPMcuzpxfg%22%2C2592000%5D%22%3B%7D'
'PATH' => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'
'SERVER_SIGNATURE' => '<address>Apache/2.4.7 (Ubuntu) Server at aloha Port 80</address>
'
'SERVER_SOFTWARE' => 'Apache/2.4.7 (Ubuntu)'
'SERVER_NAME' => 'aloha'
'SERVER_ADDR' => '127.0.0.1'
'SERVER_PORT' => '80'
'REMOTE_ADDR' => '127.0.0.1'
'DOCUMENT_ROOT' => '/var/www/html/aloha'
'REQUEST_SCHEME' => 'http'
'CONTEXT_PREFIX' => ''
'CONTEXT_DOCUMENT_ROOT' => '/var/www/html/aloha'
'SERVER_ADMIN' => 'admin@localhost'
'SCRIPT_FILENAME' => '/var/www/html/aloha/web/index.php'
'REMOTE_PORT' => '41402'
'REDIRECT_URL' => '/user/ajax'
'GATEWAY_INTERFACE' => 'CGI/1.1'
'SERVER_PROTOCOL' => 'HTTP/1.1'
'REQUEST_METHOD' => 'POST'
'QUERY_STRING' => ''
'REQUEST_URI' => '/user/ajax'
'SCRIPT_NAME' => '/web/index.php'
'PHP_SELF' => '/web/index.php'
'REQUEST_TIME_FLOAT' => 1492165297.499
'REQUEST_TIME' => 1492165297
]
<?php
$form = ActiveForm::begin(['action' => '/user/ajax']);
echo $form->field($profileHasRegioncity, 'region_id')->dropDownList($region,
[
'prompt' => 'Выбрать область...',
'onchange' => '
$.post("/user/ajax&id=' . '"+$(this).val(), function (data){
$("select#profilehasregioncity-region_id").html(data);
});'
]
);
echo $form->field($profileHasRegioncity, 'city_id')->dropDownList($city,
[
'prompt' => 'Выбрать город...',
]
);
echo '<br>' . Html::submitButton('Сохранить', ['class' => 'btn btn-success']);
$form = ActiveForm::end(); ?>
public function actionAjax()
{
if (Yii::$app->request->isAjax) {
$id = (int)Yii::$app->request->post('id');
$countCity = RegionCity::find()
->where(['parent_id' => $id])
->count();
$cities = RegionCity::find()
->where([['parent_id' => $id]])
->all();
if ($countCity > 0) {
foreach ($cities as $city) {
echo "<option value='" . $city->id . "'>$city->name_ru</option>";
}
} else {
echo "<option>-</option>";
}
}
}
2017-04-13 21:51:47 [127.0.0.1][-][-][error][yii\web\HttpException:404] exception 'yii\base\InvalidRouteException' with message 'Unable to resolve the request: user/ajax&id=2' in /var/www/html/aloha/vendor/yiisoft/yii2/base/Controller.php:127
Stack trace:
#0 /var/www/html/aloha/vendor/yiisoft/yii2/base/Module.php(523): yii\base\Controller->runAction('ajax&id=2', Array)
#1 /var/www/html/aloha/vendor/yiisoft/yii2/web/Application.php(102): yii\base\Module->runAction('user/ajax&id=2', Array)
#2 /var/www/html/aloha/vendor/yiisoft/yii2/base/Application.php(380): yii\web\Application->handleRequest(Object(yii\web\Request))
#3 /var/www/html/aloha/web/index.php(12): yii\base\Application->run()
#4 {main}
Next exception 'yii\web\NotFoundHttpException' with message 'Страница не найдена.' in /var/www/html/aloha/vendor/yiisoft/yii2/web/Application.php:114
Stack trace:
#0 /var/www/html/aloha/vendor/yiisoft/yii2/base/Application.php(380): yii\web\Application->handleRequest(Object(yii\web\Request))
#1 /var/www/html/aloha/web/index.php(12): yii\base\Application->run()
#2 {main}
2017-04-13 21:51:47 [127.0.0.1][-][-][info][application] $_COOKIE = [
'_csrf' => '58ceb4c5eb9b1b444096ffdca66037d4975556292b5ec556cb6363795423156aa:2:{i:0;s:5:\"_csrf\";i:1;s:32:\"BMExRsT4ybbmkFq_3VpcrA2WuqD3psqo\";}'
'PHPSESSID' => 'qau8p03svloqt8llnv8n5hk777'
'_identity' => 'e56c0375d8711f1a363e627c6784674d1900a5dc53c4fb4b811ada0a628b6e54a:2:{i:0;s:9:\"_identity\";i:1;s:46:\"[9,\"rxfTGlblTM0EdfKY8WLk2N2eD5TVEok9\",2592000]\";}'
]
$_SERVER = [
'REDIRECT_STATUS' => '200'
'HTTP_HOST' => 'aloha'
'HTTP_CONNECTION' => 'keep-alive'
'CONTENT_LENGTH' => '0'
'HTTP_ACCEPT' => '*/*'
'HTTP_ORIGIN' => 'http://aloha'
'HTTP_X_CSRF_TOKEN' => 'ekFKRFhMcEU4DA88Cj8kcQMjKCkzCgEaSRc6JyoNQhIPMA53KD8BKg=='
'HTTP_USER_AGENT' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36'
'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'
'HTTP_REFERER' => 'http://aloha/user/profile'
'HTTP_ACCEPT_ENCODING' => 'gzip, deflate'
'HTTP_ACCEPT_LANGUAGE' => 'uk-UA,uk;q=0.8,ru;q=0.6,en-US;q=0.4,en;q=0.2'
'HTTP_COOKIE' => '_csrf=58ceb4c5eb9b1b444096ffdca66037d4975556292b5ec556cb6363795423156aa%3A2%3A%7Bi%3A0%3Bs%3A5%3A%22_csrf%22%3Bi%3A1%3Bs%3A32%3A%22BMExRsT4ybbmkFq_3VpcrA2WuqD3psqo%22%3B%7D; PHPSESSID=qau8p03svloqt8llnv8n5hk777; _identity=e56c0375d8711f1a363e627c6784674d1900a5dc53c4fb4b811ada0a628b6e54a%3A2%3A%7Bi%3A0%3Bs%3A9%3A%22_identity%22%3Bi%3A1%3Bs%3A46%3A%22%5B9%2C%22rxfTGlblTM0EdfKY8WLk2N2eD5TVEok9%22%2C2592000%5D%22%3B%7D'
'PATH' => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'
'SERVER_SIGNATURE' => '<address>Apache/2.4.7 (Ubuntu) Server at aloha Port 80</address>
'
'SERVER_SOFTWARE' => 'Apache/2.4.7 (Ubuntu)'
'SERVER_NAME' => 'aloha'
'SERVER_ADDR' => '127.0.0.1'
'SERVER_PORT' => '80'
'REMOTE_ADDR' => '127.0.0.1'
'DOCUMENT_ROOT' => '/var/www/html/aloha'
'REQUEST_SCHEME' => 'http'
'CONTEXT_PREFIX' => ''
'CONTEXT_DOCUMENT_ROOT' => '/var/www/html/aloha'
'SERVER_ADMIN' => 'admin@localhost'
'SCRIPT_FILENAME' => '/var/www/html/aloha/web/index.php'
'REMOTE_PORT' => '42389'
'REDIRECT_URL' => '/user/ajax&id=2'
'GATEWAY_INTERFACE' => 'CGI/1.1'
'SERVER_PROTOCOL' => 'HTTP/1.1'
'REQUEST_METHOD' => 'POST'
'QUERY_STRING' => ''
'REQUEST_URI' => '/user/ajax&id=2'
'SCRIPT_NAME' => '/web/index.php'
'PHP_SELF' => '/web/index.php'
'REQUEST_TIME_FLOAT' => 1492120307.902
'REQUEST_TIME' => 1492120307
]
echo $form->field($profileHasRegioncity, 'region_id')->dropDownList($region,
[
'prompt' => 'Выбрать область...',
'onchange' => '$.post("' . Url::toRoute('user/ajax') . '", {
id: $(this).val()
},
function(data){
$("select#city_id").html(data).attr("disabled", false)
})'
]
);
echo $form->field($profileHasRegioncity, 'city_id')->dropDownList($city, [
'prompt' => 'Выбрать город...',
'id' => 'city_id',
]);
public function actionAjax()
{
if (Yii::$app->request->isAjax) {
$id = (int)Yii::$app->request->post('id');
$this->option = "<option value='0'>" . 'Выбрать область' . "</option>";
$cities = RegionCity::find()
->andWhere('parent_id=:id', [':id' => $id])
->all();
foreach ($cities as $city) {
$this->option .= '<option value="' . $city->id . '">' . $city->city . '</option>';
}
}
return $this->option;
}