我正在尝试创建一个带有几个滑块的页面。当您更改这些滑块时,我想修改从我的模型中得出的数据并更新视图。
我知道您可以通过在常规 PHP 中使用 ajax
来解决这个问题。但是我太缺乏经验了,无法在phalcon 框架内解决这个问题。 PhalconForms
不支持<input type="range" />
。所以我把它添加到我的伏特文件中:<input id="number_of_rooms" name="number_of_rooms" type="range" />
.
我假设下一步是创建一个带有滑块事件处理程序的JS
文件,该处理程序创建对我的控制器的 ajax 调用。我已经创建了事件处理程序,但是如何处理 ajax 调用给我带来了麻烦。
$('#number_of_rooms').on("change mousemove", function() {
// $.ajax({
// method: "POST",
// url: "controllers/IndexController.php",
// data: { value: this.value }
// })
console.log("value: " + this.value);
});
但是如何将这些数据返回给我的控制器以修改我的模型,我不知道。帮助将不胜感激。
让我知道我是否完全错误。谢谢!
$.ajax({
type:'POST',
url: "{{ url() }}/your_path", (you create this path in file route)
data: { value: this.value },
async: false,
dataType: "json",
success:function(result){
// the result will return from the controller
// use the result to update your modal content
},
error: function(){
// got error from controller
// Do something
}
});
在您的路线中
$backend->addPost('/your_path', array(
'controller' => 'yourcontroller',
'action' => 'yourmethod'
));
不要忘记创建您的控制器控制器并在那里添加您的方法
希望这个帮助
由于您通过 ajax 发送数据,因此 POST-ed 数据将是 json 格式,您可以使用 this->request->getPost(( 捕获它并将其传递给 getPost(( 处理程序进行修改。 例如,返回的数据将被value: val
,然后使用控制器中的$variable = $this->request->getPost('value')
捕获它