CakePHP 3.6:根据选择的下拉控件更新控件值

CakePHP 3.6:根据选择的下拉控件更新控件值,php,cakephp,onchange,Php,Cakephp,Onchange,我有以下表格: customers[id, name, surname, phone, text, balance, created] service_types[id, title, price, length, is_subscription, created, payment] customer_service_types[id, customer_id, service_type_id, price, created] 以及两者之间的关系: ServiceTypesTable.ph

我有以下表格:

customers[id, name, surname, phone, text, balance, created]

service_types[id, title, price, length, is_subscription, created, payment]

customer_service_types[id, customer_id, service_type_id, price, created]
以及两者之间的关系:

ServiceTypesTable.php:

CustomerServiceTypesTable.php:

在CustomerServiceTypes\add.ctp中,我有一个包含服务的下拉列表和一个价格字段:

echo $this->Form->control('customer_id', ['options' => $customers,'label' => 'Customer']);
echo $this->Form->control('service_type_id', ['options' => $serviceTypes, 'label' => 'Service']);
echo $this->Form->control('price', ['label' => 'Price']);
在CustomerServiceTypesController.php中:

在服务下拉值字段中添加特定服务的值:

服务建议价格:100

服务建议价格:150


但我想实现的是,当用户在下拉字段中进行选择时,用建议的价格更新价格字段。有可能实现这个服务器端吗?不使用javascript?因为我的javascript知识非常有限。如果没有,您能否根据我的问题提供一个工作示例?

进行以下更改:

add.ctp


是否要在提交时更新服务类型表中的价格列?@tphobe9312否我要更新价格控制echo$this->Form->control'price',['label'=>'price'];就我所知,在没有Javascript的下拉菜单上是不可能的。您可以通过AngularJS轻松实现这一点。@tphobe9312那么,您能为我介绍一个js解决方案吗?因为我以前没有在这样的上下文中使用过js
$this->belongsTo('Customers', [
        'foreignKey' => 'customer_id',
        'joinType' => 'INNER'
    ]);
    $this->belongsTo('ServiceTypes', [
        'foreignKey' => 'service_type_id',
        'joinType' => 'INNER'
    ]);
echo $this->Form->control('customer_id', ['options' => $customers,'label' => 'Customer']);
echo $this->Form->control('service_type_id', ['options' => $serviceTypes, 'label' => 'Service']);
echo $this->Form->control('price', ['label' => 'Price']);
public function add($customerid = null)
    {
        $customerServiceType = $this->CustomerServiceTypes->newEntity();
        if ($this->request->is('post')) {
            $customerServiceType = $this->CustomerServiceTypes->patchEntity($customerServiceType, $this->request->getData());
            if ($this->CustomerServiceTypes->save($customerServiceType)) {

                //debug($this->request->getData("customer_id"),true);
                

                $this->Flash->success(__('Success'));

                return $this->redirect(['controller' => 'customers', 'action' => 'edit', $customerid]);
            }
            $this->Flash->error(__('Fail'));
        }
        $customers = $this->CustomerServiceTypes->Customers->find('list', ['limit' => 200])->where(['Customers.id =' => $customerid]);
        $serviceTypes = $this->CustomerServiceTypes->ServiceTypes->find('list', [
        'valueField' => function ($row) {
            return $row['title'] . ' (Suggested price: ' . $row['price'] . ')';
        }
    ], ['limit' => 200]);
        $this->set(compact('customerServiceType', 'customers', 'serviceTypes'));
    }
    <div ng-app=""  ng-init='servicePrices = <?php echo json_encode($servicePrices); ?>;' >

<?php

echo $this->Form->create();

echo $this->Form->control('customer_id', ['options' => $customers,'label' => 'Customer']);

echo $this->Form->control('service_type_id', [
'options' => $serviceTypes, 'label' => 'Service',
'ng-model'=>'service_type_id'
]);

 echo $this->Form->control('price', [
'label' => 'Price',
'ng-model'=>'servicePrices[service_type_id]'
]);

echo $this->Form->submit('submit');

?>

</div>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.5/angular.min.js" ></script>
// add this
 $servicePrices = $this->CustomerServiceTypes
            ->ServiceTypes
            ->find()
            ->limit(200)
            ->combine('id','price');


    $this->set(compact('customerServiceType', 'customers', 'serviceTypes','servicePrices'));