我是CakePHP的初学者,尝试使用ajax将更改功能期间的文本框值发送到我的控制器操作。
有人可以帮助如何将值格式jquery传递给cakephp控制器。如果有示例代码可能很棒。
答案 0 :(得分:1)
假设您要将数据发送到用户控制器中名为“ajax_process”的方法。我是这样做的:
在你的视图.ctp(任何地方)
<?php
echo $this->Form->textarea('text_box',array(
'id' => 'my_text',
));
?>
<div id="ajax_output"></div>
在同一个视图文件中 - 调用事件触发器的jquery函数:
function process_ajax(){
var post_url = '<?php echo $this->Html->url(array('controller' => 'users', 'action' => 'ajax_process')); ?>';
var text_box_value = $('#my_text').val();
$.ajax({
type : 'POST',
url : post_url,
data: {
text : text_box_value
},
dataType : 'html',
async: true,
beforeSend:function(){
$('#ajax_output').html('sending');
},
success : function(data){
$('#ajax_output').html(data);
},
error : function() {
$('#ajax_output').html('<p class="error">Ajax error</p>');
}
});
}
在UsersController.php中
public function ajax_process(){
$this->autoRender = false; //as not to render the layout and view - you dont have to do this
$data = $this->request->data; //the posted data will come as $data['text']
pr($data); //Debugging - print to see the data - this value will be sent back as html to <div id="ajax_output"></div>
}
在AppController.php中禁用 ajax_process 方法的蛋糕安全性:
public function beforeFilter() {
$this->Security->unlockedActions = array('ajax_process');
}
我没有测试过任何此类代码,但它应该为您提供所需内容