duandai2178 2018-09-27 13:55
浏览 34
已采纳

CakePHP 3.6.11:将选定值从下拉列表存储到数据库

I have these 3 tables:

customers:

customerstable

services:

servicestable

customerservices:

customerservicestable

With this relation in CustomerservicesTable.php:

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

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

In Template\Customerservices\add.ctp I have a form with a dropdown and a numeric field :

<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>

in 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'));
    }

How can I replace the comment in order to save the serviceid which is selected in the dropdown control?

(secondary question is it possible to hide the price field depending on the dropdown selection?)

  • 写回答

1条回答 默认 最新

  • download12749 2018-09-27 14:43
    关注

    Just like I told you couple of minutes ago.

    Change the input like this:

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

    In your Controller:

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

    Hiding the price field depending on the selection in the dropdown seems like a job for the client side, which can be done with JavaScript. For example in 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');
    }); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示