【发布时间】: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' => 'Posts',.... 见 P 是大写 -
请发布您在浏览器中看到的 HTML
-
我已经添加了在浏览器中看到的 HTML
-
它工作正常,先生 -> jsfiddle.net/81uowjvw/1 相同的代码。
-
这就是我所说的 -> 如果您的 HTML 代码在 HTML 中工作,它也应该在 CakePHP 中工作:)。我知道一些 JS 代码禁用了提交。无论如何欢迎。