Zend Form: onchange select load another view conte

2019-04-16 05:17发布

问题:

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

I have been able to load a view of another action in the index.phtml by using:

$('#select').change(function() {
    event.preventDefault();
    var id = $(this).attr('id');
    $('#results').show();
    $('#results').load('/controller/index/' + $(this).attr('value'));
    return false;
});

However I need to pass the variables of all three select boxes and for that I alternatively used:

$('#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;
         }
    });

});

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

EDIT

the ajax action now looks like

$this->view->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);

$select1 = $this->_request->getParam('select1');
$select2 = $this->_request->getParam('select2');
$select3 = $this->_request->getParam('select3');

// DO THE OTHER STUFF AND LOGIC HERE

$results = array(
 'return1' => 'value1',
 'return2' => 'value2'
);

$this->_response->setBody(json_encode($results));

and the controller init

public function init() {
$ajaxContext = $this->_helper->getHelper('AjaxContext');
$ajaxContext->addActionContext('ajax', 'json')->initContext();
}

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

回答1:

You can do two things:

  • You can enable the layout of the action you are calling via ajax. See you have disabled layout so even if the view phtml file of the ajax action contains something, it won't show. You can enable layout, use text/html dataType instead of json and show the returned HTML somewhere.
  • Or, in the success event of the ajax call, write javascript codes to update DOM.


回答2:

Thanks @Salman for your suggestions as they lead me in the right direction and I managed to solve the problem. I managed to pass multiple parameters with the ajax .load() call by passing them as get parameters. The results of the ajaxAction could then be formatted in the ajax.ajax.phtml view and were consecutively shown within the #results div that resides in the index.phtml where the select boxes are.

controller/index.phtml

<div id="results" style="display:block;">Select all three values</div>

IndexController init and ajaxAction

public function init() {
    $ajaxContext = $this->_helper->getHelper('AjaxContext');
    $ajaxContext->addActionContext('ajax', 'html')->initContext('html');
}

public function ajaxAction() {

    $select1    = $this->_request->getQuery('select1');
    $select2    = $this->_request->getQuery('select2');
    $select3    = $this->_request->getQuery('select3');

    $form   = new Application_Form();

    // Database operations and logic

    $this->view->form       = $form;
    $this->view->array      = $somearray;
    } 
}

jquery script in index.phtml

$(document).ready(function(){

    $('.selector').change(function() {
        var select1 = $('#select1').val();
        var select2 = $('#select2').val();
        var select3 = $('#select3').val();
        if ( select1 && select2 && select3) {
            $('#results').show();               
            $('#results').load('/controller/ajax?select1=' + select1 + '&select2=' + select2 + '&select3=' + select3);
        }
    });

});

controller/ajax.ajax.phtml

<?php if ( $this->array ) : ?>
    <?php echo( $this->form ); ?>
<?php else: ?>
    Nothing found for selected values
<?php endif ?>