CakePHP 3.6.11: сохранить выбранное значение из выпадающего списка в базе данных

У меня есть эти 3 таблицы:

клиенты:

customerstable

Сервисы:

servicestable

обслуживание клиентов:

customerservicestable

С этим отношением в CustomerservicesTable.php:

$this->belongsTo('Customers')
->setForeignKey('customerid');

$this->belongsTo('Services')
->setForeignKey('serviceid');

В Template\Customerservices\add.ctp У меня есть форма с раскрывающимся списком и числовым полем:

<div class="customerservices form large-9 medium-8 columns content">
<?= $this->Form->create($customerservice) ?>
<fieldset>
<legend><?= __('Add transaction') ?></legend>
<?php
echo $this->Form->input('Transaction type',array('options' => $servicesList));
echo $this->Form->control('price');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>

в Controller\CustomerservicesController.php :

public function add($customerid = null)
{

$customerservice = $this->Customerservices->newEntity();
if ($this->request->is('post')) {
$customerservice->customerid = $customerid;
$customerservice->serviceid = //get selection from dropdown
if ($this->Customerservices->save($customerservice)) {
$this->Flash->success(__('The customerservice has been saved.'));

return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The customerservice could not be saved. Please, try again.'));
}
$this->set(compact('customerservice'));

$servicesList = TableRegistry::getTableLocator()->get('Services')->find('list');
$this->set(compact('servicesList'));
}

Как я могу заменить комментарий, чтобы сохранить serviceid который выбран в раскрывающемся списке?

(вторичный вопрос, можно ли скрыть price поле в зависимости от выпадающего выбора?)

0

Решение

Как я уже говорил пару минут назад.

Изменить input как это:

echo $this->Form->input('transaction_type',array('type'=>'select','options' => $servicesList));

В вашем Controller:

public function add($customerid = null)
{
…
$customerservice->serviceid = $this->request->getData('transaction_type');
…
}

Сокрытие price Поле в зависимости от выбора в раскрывающемся списке выглядит как работа для клиентской стороны, что можно сделать с помощью JavaScript. Например в jQuery:

$('#transaction_type').on('change', function() {
// hide element with ID #price if value of select with ID #transaction_type is `holymoly`
// and show element if value is anything else
$('#price').toggle(this.value === 'holymoly');
});
0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector