Note that there are some explanatory texts on larger screens.

plurals
  1. POZend Form: onchange select load another view content
    text
    copied!<p>In my application I have a form in controller/index that consists out of 3 select boxes. When all three boxes have a value selected I need to show additional html and extra form options in the same view based on those select values. The obvious solution seems to make an ajax call to another action that handles the database operation and creates a view and loading that view into the controller/index.phtml </p> <p>I have been able to load a view of another action in the index.phtml by using:</p> <pre><code>$('#select').change(function() { event.preventDefault(); var id = $(this).attr('id'); $('#results').show(); $('#results').load('/controller/index/' + $(this).attr('value')); return false; }); </code></pre> <p>However I need to pass the variables of all three select boxes and for that I alternatively used:</p> <pre><code>$('#select1').change(function() { var select1 = $('#select1').val(); var select2 = $('#select2').val(); var select3 = $('#select3').val(); $.ajax({ type: 'POST', dataType: 'json', url: '/controller/index/', data: { select1: select1, select2: select2, select3: select3}, success: function(result){ var return1 = result.return1; var return2 = result.return2; } }); }); </code></pre> <p>The last method works in as far that I do see the variables passed in the headers and the response contains the view, but I cant fix it that just the content of the ajax view is placed within the index view. (Ofcourse by not using AjaxContent switching, the ajax view will load but that includes the complete layout as well.) Anything that I echo in the ajax action or ajax view do not show in the index view. Any pointer would be more than welcome</p> <p><strong>EDIT</strong></p> <p>the ajax action now looks like</p> <pre><code>$this-&gt;view-&gt;layout()-&gt;disableLayout(); $this-&gt;_helper-&gt;viewRenderer-&gt;setNoRender(true); $select1 = $this-&gt;_request-&gt;getParam('select1'); $select2 = $this-&gt;_request-&gt;getParam('select2'); $select3 = $this-&gt;_request-&gt;getParam('select3'); // DO THE OTHER STUFF AND LOGIC HERE $results = array( 'return1' =&gt; 'value1', 'return2' =&gt; 'value2' ); $this-&gt;_response-&gt;setBody(json_encode($results)); </code></pre> <p>and the controller init</p> <pre><code>public function init() { $ajaxContext = $this-&gt;_helper-&gt;getHelper('AjaxContext'); $ajaxContext-&gt;addActionContext('ajax', 'json')-&gt;initContext(); } </code></pre> <p>So everything works, I can see the returned values in the response by using developer tool (network) in my browser, however I just do not know how I can use this to "update" the view</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload