【问题标题】:CakePHP 3.6.11: Update field base on dropdown valueCakePHP 3.6.11:根据下拉值更新字段
【发布时间】:2018-10-05 10:09:22
【问题描述】:

我有这张桌子:

客户[id, name, surname, phone, text, balance, created]

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

customer_service_types[id, customer_id, service_type_id, price, created]

在 CustomerServiceTypes 的 add.ctp 中,我有一个包含服务类型的下拉菜单和一个包含价格的字段。我想要的是当用户从下拉列表中选择服务类型,然后使用所选服务类型的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'));
    }

【问题讨论】:

  • 你有没有尝试做任何事??
  • @Exterminator nope 不知道如何开始,可能需要javascript?

标签: javascript php ajax cakephp cakephp-3.6


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2020-02-14
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-29
    • 1970-01-01
    相关资源
    最近更新 更多