如何在 cakephp 中通过 ajax 提交 jquery 移动弹出表单
Posted
技术标签:
【中文标题】如何在 cakephp 中通过 ajax 提交 jquery 移动弹出表单【英文标题】:how to submit a jquery mobile pop form via ajax in cakephp 【发布时间】:2015-09-13 17:31:17 【问题描述】:我一直在尝试使用 cakephp 中的 ajax 提交一个 jquery 移动表单,但该表单没有提交,我不知道可能出了什么问题,这是我的控制器代码:
public function add()
if ($this->request->is('ajax'))
$this->request->data['Room']['user_id'] = $this->Session->read('Auth.User.id');
$this->Room->User->updateAll(array("User.room_count"=>'User.room_count+1'),array('User.id'=> $this->Session->read('Auth.User.id') ));
$this->Room->create();
if ($this->Room->save($this->request->data))
$this->Session->setFlash(__('The room has been saved.'));
$this->render('AjaxRoom','ajax');
else
$this->render('AjaxRoom','ajax');
$users = $this->Room->User->find('list');
$this->set(compact('users'));
这是我的视图代码:
<div id="sent" style="display:none;">Updating...</div>
<div id="success"></div>
<script type="text/javascript">
//<![CDATA[
$(document).ready(function ()
$("#RoomIndexForm").bind("submit", function (event)
$.ajax(
async:true,
beforeSend:function (XMLHttpRequest) $("#sent").show(500),
complete:function (XMLHttpRequest, textStatus) $("#sent").hide(500);$("#TalkViewForm").each (function()this.reset(););
//$("#refresh").load(location.href + " #refresh");
,
data:$("#RoomIndexForm").serialize(),
dataType:"html",
success:function (data, textStatus) //$("#success").html(data);
,
type:"POST",
url: "<?php echo $this->Html->url(array('controller' => 'rooms', 'action' => 'add')); ?>");
return false;
);
);
//]]>
</script>
<div data-role="popup" id="popupRoom" data-theme="a" class="ui-corner- all">
<?php echo $this->Form->create('Room',array('role'=>'form','url'=>array('controller'=>'r ooms', 'action'=>'add'))); ?>
<div style="padding:10px 20px;">
<h3>Add Room</h3>
<?php echo $this->Form->input('name', array(
'label' => false,
'placeholder'=>'Room Name'));
?>
<?php
echo $this->Form->input('description', array(
'label' => false,
'type' => 'textarea',
'maxlength'=>"140",
'placeholder'=>'Describe Your room briefly.'));
?>
<?php
$accountValues = array();
foreach ($categories as $key => $value)
$accountValues[$value] = $value;
$categories = $accountValues;
echo $this->Form->select('category',array(
'options' =>$categories), array(
'label' => false,
'empty' => false
));
?>
<?php
//echo $this->Form->input('filename', array('type' => 'file','label'=>'Upload an Image'));
//echo $this->Form->input('file_dir', array('type' => 'hidden'));
?>
<?php
echo $this->Form->input('is_private', array(
'label' => 'Do you want a private Room ? If No, just ignore this field.',
'type'=>'checkbox',
));
?>
<?php echo $this->Form->submit('Create',array('class'=>'ui-btn ui-corner-all ui-shadow ui-btn-b ui-btn-icon-left ui-icon-check')); ?>
</div>
<?php echo $this->Form->end(); ?>
</div>
请问我如何实现这个功能?欢迎任何帮助。
【问题讨论】:
【参考方案1】:我认为 Html Helper 不支持 url:
$this->Html->url(array('controller' => 'rooms', 'action' => 'add'))
尝试改用这个:
$this->Html->link(['controller' => 'Rooms', 'action' => 'add'])
另一种方法是 url builder (http://book.cakephp.org/3.0/en/views/helpers/url.html):
$this->Url->build(['controller' => 'Rooms', 'action' => 'add'])
【讨论】:
以上是关于如何在 cakephp 中通过 ajax 提交 jquery 移动弹出表单的主要内容,如果未能解决你的问题,请参考以下文章
在 Woocommerce 3 中通过 ajax 提交并在结帐时创建订单