How to access variable from one view to another view

From the code as below, I have tried to filter result when the text input (searchdata) has some value and the search button is clicked.

I am using Datalist variable to display the search result on view. I am not sure how can I access Datalist variable (in indexAction) from filterAction to display filtered result on view. Could you please suggest?

View (views/test/index.volt) :

<!-- searching data -->
<div class="mdl-textfield fixedsize mdl-js-textfield mdl-textfield--floating-label" autocomplete="off">
     {{ text_field('searchdata','autocomplete':'off', 'class': 'mdl-textfield__input searchfield') }}
</div>
<label class="search_buttondiv mdl-button mdl-js-button mdl-button--icon" for="sample6">
     <i class="material-icons" id="search-icon1">search</i>
</label>

<!--  display data -->
<table class="userlist mdl-data-table mdl-js-data-table mdl-shadow--2dp">
      <thead>
             <tr>
               <th class="mdl-data-table__cell--non-numeric">UserID</th>
               <th>username</th>
             </tr>
       </thead>
       <tbody>             
             {% for i,Datalist in Data %}
              <tr>
                 <td>
                    {{ Data['_id'] }}
                 </td>
                 <td>
                     {{ Data['name'] }}
                 </td>
             </tr>
             {% endfor %}
       </tbody>
 </table>

 <script>
     $('#search-icon1').click(function(){
           $(function(){
              $.ajax({
                 url: '<?php echo  $this->url->get("test/filter");?>',
                 type: 'post',
                 data: {userid : document.getElementsByName('searchdata')[0].value},
                 dataType:"json",
                 success: function(){
                    console.log("success filtering");
                 },
                 error: function(xhr, ajaxOptions, thrownError){
                    console.log(thrownError);
                 }
              });
           });
});
</script>

Controller (controllers/testController.php) :

<?php

 class testController extends ControllerBase
 {
     public function indexAction()
     {
           $app = new AppController;
           $result = $app->getDatafromUser(null);
           $this->view->setVar('Datalist', $result);
     }

     public function filterAction()
     {
           $app = new AppController;
           $result = $app->getDatafromUser($_POST['userid']);
           $this->view->setVar('Datalist', $result);   /*How can I access Datalist variable from indexAction?*/
     }
}


1.9k
edited Jun '17

Hello, I'm not sure because I never try to implement ajax action with phalcon yet.

Your "test/filter" return is in Json and you need to parse json data to display in your table. For me the only solution is JS for now.

I hope it's help you ;o))

P.S. : if filterAction should be only accessed by ajax, you should implement this in your code :

if ($request->isPost() && $request->isAjax()) {
    // Check whether the request was made with Ajax
        echo 'Request was made using POST and AJAX';
} else {
    $this->flash->error('Forbidden Action');
}

More info about request there : https://docs.phalconphp.com/en/3.2/request

Some more clue : https://forum.phalconphp.com/discussion/22/the-best-way-for-json-response-

https://stackoverflow.com/questions/26613343/phalconphp-binding-view-with-ajax-using-volt-template

Maybe there is another way with html return but I'm not enough experienced with phalcon views. Somethings like returning a partial view with this content :

{% for i,Datalist in Data %}
    <tr>
        <td>
            {{ Data['_id'] }}
        </td>
        <td>
            {{ Data['name'] }}
        </td>
    </tr>
{% endfor %}
edited Jun '17

First - don't use $_POST, use $this->request->getPost()

Ajax is ajax. You need to return json and in js response the content in body. Phalcon is SERVER SIDE, AJAX/JS is CLIENT SIDE. You can't expect phalcon when you do ajax request to change content of page which is already rendered.

Thanks elroliv for your suggestion. I will apply your hint into my code. :)

Thanks Wojciech Ślawski. ok, I will replace this one. :)

It means that variable in view (volt file) can be populated only one time (when this page is loaded) and we can't change this value from controller. Is this right? I have tried to request the server side (controller) to get the new information of variable and then display the new one to my view.

No, it means that using ajax you can't render new page and display it using php without using js.



1.9k
edited Jun '17

No, it means that using ajax you can't render new page and display it using php without using js.

It is possible to render a html part and send it back to an ajax call where datatype is html ?

// jQuery async request
    $.ajax(
    {
        url: urlWithContent,
        dataType: "html",
        success: function(data) {
            divToUpdate.innerHTML = data;
        },
        error: function(e) 
        {
            alert('Error: ' + e);
        }
    });

Yea you can do it like this sure. I mean you can't update rendered page using php during ajax request. You need to return js/html/whatever and do it in JS :)



1.9k

Yea you can do it like this sure. I mean you can't update rendered page using php during ajax request. You need to return js/html/whatever and do it in JS :)

Thanks because phalcon render is not my friend and I was in doubt about this other solution ;o))