【问题标题】:CakePHP form submit not workingCakePHP 表单提交不起作用
【发布时间】:2015-02-05 09:28:51
【问题描述】:

我刚刚开始使用 CakePHP,我想创建一个表单,当它被提交时它会调用控制器中的一个函数。我使用的是 CakePHP 2.6 版

我目前拥有的代码是:

查看:

<div class="modal-body">
    <?php echo $this->Form->create('Tweet', array('url' => array('controller' => 'posts', 'action' => 'postTweet'))); ?>
    <?php echo $this->Form->textarea('Tweet', array('class' => 'form-control','rows' => '3', 'placeholder' => "what's happening?", 'maxlength' => '140', 'label' => false)); ?>
    <?php echo $this->Form->button('Close', array('class' => 'btn btn-default', 'data-dismiss' => 'modal', 'type' => 'button'));?>
    <?php echo $this->Form->submit('Tweet', array('class' => 'btn btn-primary', 'type' => 'submit', 'div' => false)); ?>
    <?php echo $this->Form->end(); ?>
</div>

PostsController:

    public function postTweet(){
        //check if post is being made
        if ($this->request->is('post')) {
            //do something
        }
    }

页面上的 HTML:

<form action="/posts/postTweet" id="TweetIndexForm" method="post" accept-charset="utf-8">
    <div style="display:none;">
        <input name="_method" value="POST" type="hidden">
    </div>              
    <textarea name="data[Tweet][Tweet]" class="form-control" rows="3" placeholder="what's happening?" maxlength="140" id="TweetTweet"></textarea>
    <button class="btn btn-default" data-dismiss="modal" type="button">Close</button>            
    <input class="btn btn-primary" value="Tweet" type="submit">              
</form>          

问题是当我单击提交按钮时没有任何反应,我在 Firebug 中进行了检查,控制台选项卡中没有错误,网络选项卡中没有进行 POST,并且错误日志中也没有记录任何错误。任何帮助都将非常感谢。

编辑: 解决了我的 JavaScript 阻止表单提交感谢您的帮助!

【问题讨论】:

  • 我认为你使用 -> array('controller' =&gt; 'Posts', .... 见 P 是大写
  • 请发布您在浏览器中看到的 HTML
  • 我已经添加了在浏览器中看到的 HTML
  • 它工作正常,先生 -> jsfiddle.net/81uowjvw/1 相同的代码。
  • 这就是我所说的 -> 如果您的 HTML 代码在 HTML 中工作,它也应该在 CakePHP 中工作:)。我知道一些 JS 代码禁用了提交。无论如何欢迎。

标签: php cakephp


【解决方案1】:

使用提交方式 ->

echo $this->Form->submit('Tweet', array('label' => false, 'name' => 'submit', 'class' => 'grayBTN', 'title' => '', 'alt' => 'Submit', 'error' => false));

效果很好,我一直在用它。

您提交的问题是您的代码中使用了 'type' =&gt; 'submit' 并且您还指定了 $this-&gt;Form-&gt;submit

如果您指定 Button 的类型为 submit 那么它可以像 ->

echo $this->Form->button('Submit Form', array('type' => 'submit'));

但在你的情况下你有 $this->Form->submit.. 所以你不需要明确指定它。

【讨论】:

  • 是的,我一直在尝试不同的东西,当我试图使用 $this->Form->button 让它工作时,'type' => 'submit' 就留在了那里,我'现在删除了它,仍然有同样的问题
  • @user3758298,如果您的 HTML 代码在 HTML 中工作,它也应该在 CakePHP 中工作 :)。请再次检查
【解决方案2】:

试试这个

echo $this->Form->end(array('label' => 'Tweet','div' => false,'class' => 'btn btn-primary'));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-28
    • 2016-04-06
    • 2017-02-21
    • 2014-03-21
    • 2017-05-24
    • 2014-07-08
    • 2017-10-03
    相关资源
    最近更新 更多