【发布时间】:2015-03-28 09:59:30
【问题描述】:
我是 cakephp 新手。我正在尝试制作一个简单的 ajax 请求来修改屏幕的一部分。我在网上找到了几个例子,但我无法将它们混合在一起! 当我单击按钮 btnNewPhon 时,我想将输入 txtPhon 的内容发送到控制器 adminController。然后在视图中,div dataToShow 将被更新以显示控制器发送的数据。
我快到了,但我不知道如何将输入 phonID 和 phonTxt 的内容发送到控制器,我不知道如何获取控制器中的数据。这是我的代码。
查看index.ctp:
<div id="dataToshow"></div>
<input id="phonID" type="text">
<input id="phonTxt" type="text">
<button id="btnNewPhon">New</button>
Ajax 视图 ajax_response.ctp:
<?php echo $content; ?>
Javascript admin.js(包含在视图中):
$(function() {
$('#btnNewPhon').click(function() {
a = $("#txtPhon").val ;
$.ajax({
type:"POST",
datatype:'json',
cache: false,
data: <<< I don't know what to put here >>>,
url:"admin/addPhon",
update:"#dataToshow"
});
});
});
控制器adminController.php:
public function addPhon() {
if ($this->request->is('ajax')) {
// Treatment of the new data here
//create current date in chosen format
$new_data = '##'.$this->request->param('phonID').'-'.$this->request->param('phonTxt').'##';
//set current date as content to show in view
$this->set('content', $new_data);
//render spacial view for ajax
$this->render('ajax_response', 'ajax');
}
}
谢谢!!!
【问题讨论】:
-
您还应该考虑使用适当的扩展名:`url:"admin/addPhon.json"
标签: jquery ajax cakephp view controller