我刚开始使用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>
帖子控制器:
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 是大写的。 - Pratik Joshi