Benix89 Benix89 - 2 months ago 8
Javascript Question

Redirect to the homepage after the wrong answer

I am creating a payment method based on questions / answers in which the correct answer to a random question from a pre-defined question pool is valid as payment.

I wish if the user gives the wrong answer, after the appearance of the message "wrong answer", was "redirected" in the homepage.

This is the code that validates the answer if right and wrong:

public function validate()
{
foreach (Mage::getModel('emme_question/question')->load(1)->getSelectedAnswersCollection() as $answer)
{
if ($answer->getIsCorrect())
{
if ($answer->getId() == $_POST['my_custom_answer'])
{
Mage::getSingleton('core/session')->addSuccess('Risposta esatta');
} else
{
Mage::throwException('Risposta sbagliata!');
}
}
}
}


But being that everything I do after Mage :: ThrowException will not run because the exception interrupts the normal flow of the code.
In addition, because the validation starts from an AJAX call in the checkout, I can not simply echo of html or javascript, but I have to handle the redirect directly to the client side.

I should readjust this part of the code (the skin / frontend / base / default scripts / js / opcheckout.js starting with line 875) so that in addition to just alert also face the redirect.

Code:

/*
* if there is an error in payment, need to show error message
*/
if (response.error) {
if (response.fields) {
var fields = response.fields.split(',');
for (var i=0;i<fields.length;i++) {
var field = null;
if (field = $(fields[i])) {
Validation.ajaxError(field, response.error);
}
}
return;
}
if (typeof(response.message) == 'string') {
alert(response.message);
} else {
alert(response.error);
}
return;
}

checkout.setStepResponse(response);

//checkout.setPayment();
},

initWhatIsCvvListeners: function(){
$$('.cvv-what-is-this').each(function(element){
Event.observe(element, 'click', toggleToolTip);
});
}
}

var Review = Class.create();
Review.prototype = {
initialize: function(saveUrl, successUrl, agreementsForm){
this.saveUrl = saveUrl;
this.successUrl = successUrl;
this.agreementsForm = agreementsForm;
this.onSave = this.nextStep.bindAsEventListener(this);
this.onComplete = this.resetLoadWaiting.bindAsEventListener(this);
},

save: function(){
if (checkout.loadWaiting!=false) return;
checkout.setLoadWaiting('review');
var params = Form.serialize(payment.form);
if (this.agreementsForm) {
params += '&'+Form.serialize(this.agreementsForm);
}
params.save = true;
var request = new Ajax.Request(
this.saveUrl,
{
method:'post',
parameters:params,
onComplete: this.onComplete,
onSuccess: this.onSave,
onFailure: checkout.ajaxFailure.bind(checkout)
}
);
},

resetLoadWaiting: function(transport){
checkout.setLoadWaiting(false, this.isSuccess);
},

nextStep: function(transport){
if (transport && transport.responseText) {
try{
response = eval('(' + transport.responseText + ')');
}
catch (e) {
response = {};
}
if (response.redirect) {
this.isSuccess = true;
location.href = response.redirect;
return;
}
if (response.success) {
this.isSuccess = true;
window.location=this.successUrl;
}
else{
var msg = response.error_messages;
if (typeof(msg)=='object') {
msg = msg.join("\n");
}
if (msg) {
alert(msg);
}
}

if (response.update_section) {
$('checkout-'+response.update_section.name+'-load').update(response.update_section.html);
}

if (response.goto_section) {
checkout.gotoSection(response.goto_section, true);
}
}
},

isSuccess: false
}


Can you give me a hand?

Answer

I solved

if (response.error) {
    if (response.fields) {
        var fields = response.fields.split(',');
        for (var i=0;i<fields.length;i++) {
            var field = null;
            if (field = $(fields[i])) {
                Validation.ajaxError(field, response.error);
            }
        }
        return;
    }
    if (typeof(response.message) == 'string') {
        alert(response.message);
    } else {
        alert(response.error);
window.location = 'http://demo.mmstore.net/index.php/';
    }
    return;
}
Comments