Two functions in one call

85 views Asked by At

I would like to know if it's possible to do something like that:

 x$().xhr('<?php echo $this->createUrl('survey/index', array('id'=>$model->skey)); ?>', {
                method: 'POST',
                async: true,
                data: urlstringserialize(serialize(document.getElementById("token-form"))),
                callback: function() {respOK(this.responseText);}
            });
            x$().xhr('<?php echo $this->createUrl('survey/saveBrowser', array('id'=>$model->skey,'browser'=>$browser->getBrowser(),'platform'=>$browser->getPlatform())); ?>', {
                method: 'POST',
                async: true,
                data: urlstringserialize(serialize(document.getElementById("token-form"))),
                callback: function() {respOK(this.responseText);}
            });

I need to call two different controller functions, one after the other. In this way only the first one is executed.

2

There are 2 answers

1
KroniX On

If you want to Execute the second request after the first one, you just need to make the request inside the Callback.

 x$().xhr('<?php echo $this->createUrl('survey/index', array('id'=>$model->skey)); ?>', {
        method: 'POST',
        async: true,
        data: urlstringserialize(serialize(document.getElementById("token-form"))),
        callback: function() {
            respOK(this.responseText);
            x$().xhr('<?php echo $this->createUrl('survey/saveBrowser', array('id'=>$model->skey,'browser'=>$browser->getBrowser(),'platform'=>$browser->getPlatform())); ?>', {
                method: 'POST',
                async: true,
                data: urlstringserialize(serialize(document.getElementById("token-form"))),
                callback: function() {respOK(this.responseText);}
            });
});

The Problem of the only the first one get called can depend on the Library you are using that seems to be "x$".

0
Javier Eslava On
 function respOK(data) {
        <?php if($browser->getBrowser() == Browser::BROWSER_IE) { ?>
        <?php if($campaignCenter->campaign_id == 326) { ?>
            x$("style").remove(".type");
        <?php } ?>
            x$('div.surveyform').html(data);
        <?php } else  { ?>
        <?php if($campaignCenter->campaign_id == 326) { ?>
            x$("style").remove(".type");
        <?php } ?>
            x$('div.surveyform').tween({opacity:0, duration: 200}, function() {x$('div.surveyform').html(data); x$('div.surveyform').tween({opacity: 1, duration: 200});});
        <?php } ?>
    }