gpt4 book ai didi

php - 如何在cakephp中通过ajax提交jquery移动pop表单

转载 作者:行者123 更新时间:2023-12-01 05:40:24 25 4
gpt4 key购买 nike

我一直在尝试在cakephp中使用ajax提交jquery移动表单,但是该表单没有提交,我不知道可能出了什么问题,这是我的 Controller 代码:

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'));
}

这是我的 View 代码:

<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>

请问如何实现这个功能?欢迎任何帮助。

最佳答案

Html Helper 不支持我认为的 url:

$this->Html->url(array('controller' => 'rooms', 'action' => 'add'))

尝试使用这个:

$this->Html->link(['controller' => 'Rooms', 'action' => 'add'])

另一种方法是 url 构建器 ( http://book.cakephp.org/3.0/en/views/helpers/url.html ):

$this->Url->build(['controller' => 'Rooms', 'action' => 'add'])

关于php - 如何在cakephp中通过ajax提交jquery移动pop表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31078641/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com