CakePHP 3.6.11:根据下拉值更新字段

时间:2018-10-05 10:09:23

标签: javascript php ajax cakephp cakephp-3.6

我有这张桌子:

  

客户[id,姓名,姓氏,电话,文字,余额,已创建]

     

service_types [id,标题,价格,长度,is_subscription,已创建]

     

customer_service_types [id,customer_id,service_type_id,价格,已创建]

add.ctp的CustomerServiceType中,我有一个包含服务类型的下拉列表和一个包含价格的字段。我想要的是用户从下拉菜单中选择一种服务类型,然后使用所选服务类型的price更新价格字段。

以下是现有代码:

add.ctp

<fieldset>
        <legend><?= __('Add Customer Service Type') ?></legend>
        <?php
            echo $this->Form->control('customer_id', ['options' => $customers]);
            echo $this->Form->control('service_type_id', ['options' => $serviceTypes]);
            echo $this->Form->control('price');
        ?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>

CustomerServiceTypesController.php(添加功能)

public function add()
    {
        $customerServiceType = $this->CustomerServiceTypes->newEntity();
        if ($this->request->is('post')) {
            $customerServiceType = $this->CustomerServiceTypes->patchEntity($customerServiceType, $this->request->getData());
            if ($this->CustomerServiceTypes->save($customerServiceType)) {
                $this->Flash->success(__('The customer service type has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The customer service type could not be saved. Please, try again.'));
        }
        $customers = $this->CustomerServiceTypes->Customers->find('list', ['limit' => 200]);
        $serviceTypes = $this->CustomerServiceTypes->ServiceTypes->find('list', ['limit' => 200]);
        $this->set(compact('customerServiceType', 'customers', 'serviceTypes'));
    }

1 个答案:

答案 0 :(得分:0)

您可以创建一个js函数,该函数使用选择框的onchange属性调用,然后在获得结果后立即更改输入字段值。

作为参考,您可以看看此Changing the value of Input field when user select option from select box