【问题标题】:CakePHP: How to detect an html event (like onChange) and call a specific controllerCakePHP:如何检测 html 事件(如 onChange)并调用特定控制器
【发布时间】:2018-07-12 22:49:40
【问题描述】:

我正在学习 CakePHP,这周刚开始。 到目前为止,我在从 Tables 和 BelongToMany 关联中检索和显示数据方面进展顺利。

现在我遇到了一个问题: 如何检测表单的特定字段更改。

我有什么:

 <?php foreach ($visit->contacts as $contact): ?>
        <tr>
         // SOME CODE to display all contact info
       <td>
        // Show a description about the contact in the visit (like 
       //his comments, his opinions, etc), and allow to change 
       //anytime without to send to a Form page
         <?= $this->Form->create($contact->_joinData,[
         'url'=>['action' => 'updateDescription',$visit->id,$contact->id]]) 
        ?>

<?php
     echo $this->Form->control('description',['label'=>false, 
    'type'=>'textbox', 'onchange'=> $this->Form->submit()]);

    ?>

 </td> // AND MORE code; end foreach

在此页面中,我想检测 FORM 中的描述更改,将其发送到特定控制器更新表 visit_contacts 描述列匹配 visit_id 和 contact_id 行。

问题是如何检测更改,我想在用户使用 Enter 或 out 焦点完成编辑时检测并应用更改,我使用 OnChange 只是为了测试事件但不起作用。

我不想使用提交按钮。

如何检测html事件并调用特定的控制器

提前致谢

【问题讨论】:

  • 为此目的使用 ajax

标签: php html cakephp-3.0


【解决方案1】:

好吧,我发现我的代码是正确的,除了 onChange 函数的调用:

而不是拥有

'onchange'=> $this->Form->submit()]

应该是

'onchange'=> $this->Form->submit]

因此当有人更改文本时它会提交给我的控制器。

希望这可以帮助遇到同样问题的人

【讨论】:

    猜你喜欢
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    • 2013-10-19
    • 1970-01-01
    • 2021-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多