我是CakePHP的初学者,在更改函数期间尝试将textbox值发送到我使用ajax的控制器操作。
有人能帮助如何将值形式jquery传递给cakephp控制器吗?如果有例子的话,代码可能会很棒。
发布于 2014-12-23 22:24:08
假设您希望将数据发送到用户控制器中的一个名为“”的方法。我是这样做的:
在您看来,.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>
}
在ajax_process方法中禁用AppController.php的安全:
public function beforeFilter() {
$this->Security->unlockedActions = array('ajax_process');
}
我还没有测试过这些代码,但是它应该会给出您所需要的
https://stackoverflow.com/questions/27624531
复制相似问题