tags:

views:

25

answers:

1

I'm modifying an existing system which uses Smarty. This particular problem is causing a major stumbling block as I just can't find a way around it.

I have a data-grid, each record has an action which calls ?module=the_module&action=the_action.

My corresponding function function the_module_the_action($postData, $getData) is used to perform the particular request.

The request for this module comes through from a jQuery $.ajax({}) request, simply because I don't want to loose the search form variables. So on success, I'll redirect the page using document.location='index.php?module=&action='.

This is fine as it allows me to detect that the action has been successful whilst maintaining the search filters on the grid.

But, I have a problem when the user action fails. The method the_module_the_action($postData, $getData) return echo 'success'; exit() on success but on a failure it should print out the Smarty template i.e. details of the record.

I believe the problem occurs because of a call through Ajax. The template is being served but because it is a call made using Ajax it isn't displayed.

How can I get around this problem?

Update:-

This is my function which Ajax calls:

public function module_action($post_data) {
    $object=new Class((int)$post_data["id"]);
    if($object->method()) {
        echo "success";
        exit();
    }
    else {              
        $this->assignToSmarty('_someSmartyVar', $someData);
        $this->assignToSmarty('_data', $class->getData());
            echo "failed";
    }
}

The Ajax used to call this is:-

$(document).ready(function() {          
    $(".revokeButton").click(function(){
        var selected=$(this).attr('id');
        var filterBu = $('#filter_bu').val();
        var filterJc = $('#filter_jc').val();
        if(confirm("Are you sure you want to r.....")) {
            $.ajax({
                beforeSend: function() { },
                url: 'index.php?module=module&action=action',
                type: 'POST',
                data: ({ bid:selected }),
                cache: false,
                success: function(data, textStatus) { 
                    if(data == 'success') {
                        success_message = 'success';
                    } else if(data == 'failed') {
                        success_message = 'failed';
                    }
                    document.location='index.php?module=module&message='+success_message+'&filter_bu='+filterBu+'&filter_jc='+filterJc;
                }
            });
        }
    });
});

The success and failure are echoing out successfully, but the smarty stuff isn't.

+1  A: 

I see two problems. First of all, you seem to have forgotten the smarty->display() function call in the failure leg of the module_action function. Secondly, you wrote:

if(data == 'success') {
    success_message = 'success';
} else if(data == 'failed') {
    success_message = 'failed';
}

This will always only output success or failure. You probably want to do something more like the following:

if(data == 'success') {
    success_message = 'success';
} else {
    // if got here, 'data' doesn't contain "success!", so it must contain
    // something else, likely error data. You could either just display
    // data as it is or first do some sort of check to ensure that it
    // does, in fact, contain error data, and then display it.
    success_message = 'error!<br \>\n' + data; 
}
eykanal