0
votes

I'm trying to create a page with a few sliders on it. When you change these sliders I would like to modify the data that comes out of my model and update the view.

I know you can solve this by using ajax in regular PHP. But I'm too inexperienced to solve this within the phalcon framework. Phalcon\Forms Doesn't support <input type="range" />. So I added it in my volt file: <input id="number_of_rooms" name="number_of_rooms" type="range" />.

I assume the next step is to create a JS file with a slider event handler which creates a ajax call to my controller. I've created the event handler, but how to approach the ajax call is giving me troubles.

$('#number_of_rooms').on("change mousemove", function() {
    // $.ajax({
    //     method: "POST",
    //     url: "controllers/IndexController.php",
    //     data: { value: this.value }
    // })
    console.log("value: " + this.value);
});

But how to get this data back to my controller to modify my model, I don't know. Help would be greatly appriciated.

Let me know if I am approaching this completely wrong. Thanks!

2

2 Answers

0
votes
    $.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 
        }
    });

In your route

$backend->addPost('/your_path', array(
    'controller' => 'yourcontroller',
    'action' => 'yourmethod'
));

Don't forget create yourController Controller and add yourmethod in there

hope this help

0
votes

Since you send the data via ajax then the POST-ed data will be json format, you can capture it with this->request->getPost() and pass it into your getPost() handler to modify. for example data returned will be value: val, then capture it using $variable = $this->request->getPost('value') in your controller