Im not sure this is the proper forum for this question but here it goes...

I have an script that uses ajax and jquery to send some data to another server. It uses a call back function

success: function

I want to use drupal_set_message to then display the result of the function.

function(var) { ...some code }

I have not found a way that works in doing this. I am not very familiar with ajax or jquery and am not sure how to make this work or if it is even possible. It would seem that the function would need to be set as a variable i.e.

$somevar = function

and then something like this?

drupal_set_message(t('%somevar.', array('%somevar')));

Any help would be appreciated.

Comments

nevets’s picture

drupal_set_message() is part of sever side environment (PHP code).

Your function though is client side and javascript so it can not use drupal_set_message();

If your theme places the messages from drupal_set_message in a div with an id or class you can do something like the following. The success function should expect two arguments (http://docs.jquery.com/Ajax/jQuery.ajax#options) and I am going to assume the div has an id of 'message' and that the response returns a text string or html.

success: function (data, textStatus) {
  $('#message').append(data);
}
cbarilla’s picture

That makes sense. I didnt even think of that. Like I said not alot of experience with client side code (ajax and Javascript). I am not sure how to use your example with the code I am trying to hack.

All it is doing is sending some info (an email address) to another server (contact manager/newsletter) and getting a message back)

Below is exactly how the success line reads in the script

success: mce_success_cb

and this is the function

function mce_success_cb(resp){
    mce_jQuery('#mce-success-response').hide();
    mce_jQuery('#mce-error-response').hide();
    if (resp.result=="success"){

        mce_jQuery('#mce-'+resp.result+'-response').show();
        mce_jQuery('#mce-'+resp.result+'-response').html(resp.msg);
        mce_jQuery('#mc-embedded-subscribe-form').each(function(){
            this.reset();
    });
    } else {
        var index = -1;
        var msg;
        try {
            var parts = resp.msg.split(' - ',2);
            if (parts[1]==undefined){
                msg = resp.msg;
            } else {
                i = parseInt(parts[0]);
                if (i.toString() == parts[0]){
                    index = parts[0];
                    msg = parts[1];
                } else {
                    index = -1;
                    msg = resp.msg;
                }
            }
        } catch(e){
            index = -1;
            msg = resp.msg;
        }
        try{
            if (index== -1){
                mce_jQuery('#mce-'+resp.result+'-response').show();
                mce_jQuery('#mce-'+resp.result+'-response').html(msg);           
            } else {
                err_id = 'mce_tmp_error_msg';
                html = '<div id="'+err_id+'" style="'+err_style+'"> '+msg+'</div>';
               
                var input_id = '#mc_embed_signup';
                var f = mce_jQuery(input_id);
                if (ftypes[index]=='address'){
                    input_id = '#mce-'+fnames[index]+'-addr1';
                    f = mce_jQuery(input_id).parent().parent().get(0);
                } else if (ftypes[index]=='date'){
                    input_id = '#mce-'+fnames[index]+'-month';
                    f = mce_jQuery(input_id).parent().parent().get(0);
                } else {
                    input_id = '#mce-'+fnames[index];
                    f = mce_jQuery().parent(input_id).get(0);
                }
                if (f){
                    mce_jQuery(f).append(html);
                    mce_jQuery(input_id).focus();
                } else {
                    mce_jQuery('#mce-'+resp.result+'-response').show();
                    mce_jQuery('#mce-'+resp.result+'-response').html(msg);
                }
            }
        } catch(e){
            mce_jQuery('#mce-'+resp.result+'-response').show();
            mce_jQuery('#mce-'+resp.result+'-response').html(msg);
        }
    }
}

The function basically is giving a message that indicates whether or not the information was received and some additional info. Obviously I am just trying to get the result message to display in the drupal message div like you anticipated.

A little more direction would be appreciated.

nevets’s picture

The code puts the message with

        mce_jQuery('#mce-'+resp.result+'-response').show();
        mce_jQuery('#mce-'+resp.result+'-response').html(resp.msg);

The show() makes the area visible, the html() replaces the elements current html with resp.msg.

It uses two elements '#mce-success-response' and '#mce-error-response' to display the message.

cbarilla’s picture

I gathered that that is what it was doing. But right now the message is being rendered in a block. I want it to render in the drupal message div. I don't see how to make that happen with the snippet you provided previously or any other way. This function gets called and depending on success or error it returns a message. How do I get the message it returns to display in the message div? Sorry if I am missing something...I really do appreciate the help.

nevets’s picture

Where ever there are lines like

        mce_jQuery('#mce-'+resp.result+'-response').show();
        mce_jQuery('#mce-'+resp.result+'-response').html(resp.msg);  ***

replace them with

        mce_jQuery('#message').append(resp.msg);

You need to use the parameter to append (it's not always the same variable).
Also #message assumes your message div has the id 'message'.

cbarilla’s picture

Thanks for the help. I now have the success message displaying inside the correct DIV. I have tried several ways to add some css classes to
mce_jQuery('#message').append(resp.msg);

none of which worked. After some reading on jQuery and ajax I am wondering if the right way would be through .append? I am not sure how though once again help would be appreciated.

There is also another error message that is giving me trouble as it is being rendered in a different way and I haven't been able to figure out how to send it to the message div. At least I am getting my feat wet with ajax and jQuery finally.

var fnames = new Array();var ftypes = new Array();fnames[0]='EMAIL';ftypes[0]='email';fnames[1]='FNAME';ftypes[1]='text';fnames[2]='LNAME';ftypes[2]='text';var err_style = '';
try{
    err_style = mc_custom_error_style;
} catch(e){
    err_style = 'margin: 1em 0 0 0; padding: 1em 0.5em 0.5em 0.5em; background: rgb(255, 238, 238) none repeat scroll 0% 0%; font-weight: bold; float: left; z-index: 1; width: 80%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: rgb(255, 0, 0);';
}
var mce_jQuery = jQuery.noConflict();
mce_jQuery(document).ready( function($) {
  var options = { errorClass: 'mce_inline_error', errorElement: 'div', errorStyle: err_style, onkeyup: function(){}, onfocusout:function(){}, onblur:function(){}  };
  var mce_validator = mce_jQuery("#mc-embedded-subscribe-form").validate(options);
  options = { url: 'http://post-json?u=2c23df03a247f5fe58ec8=?', type: 'GET', dataType: 'json', contentType: "application/json; charset=utf-8",
                beforeSubmit: function(){
                    mce_jQuery('#mce_tmp_error_msg').remove();
                    mce_jQuery('.datefield','#mc_embed_signup').each(
                        function(){
                            var txt = 'filled';
                            var fields = new Array();
                            var i = 0;
                            mce_jQuery(':text', this).each(
                                function(){
                                    fields[i] = this;
                                    i++;
                                });
                            mce_jQuery(':hidden', this).each(
                                function(){
                                if ( fields[0].value=='MM' && fields[1].value=='DD' && fields[2].value=='YYYY' ){
                                this.value = '';
} else if ( fields[0].value=='' && fields[1].value=='' && fields[2].value=='' ){
                                this.value = '';
} else {
                                    this.value = fields[0].value+'/'+fields[1].value+'/'+fields[2].value;
                                }
                                });
                        });
                    return mce_validator.form();
                },
                success: mce_success_cb

            };

The code validates the info entered in the form and either goes to "success: mce_success_cb" or the preceding error. But I am sure you could see that.

As far as I can tell the code creates a div with a class of 'mce_inline_error' and renders it inline. I want to send this messgae to the same message div as the rest but am not sure how to modify this code to do it or if is possible.

cbarilla’s picture

After some research I am guessing that I need something like this:

document.getElementById('message').appendChild(options);

Does this make sense? Where would this code be inserted? What other problems might occur i.e. double render of the error message?

nevets’s picture

Not jQuery code and there not as portable between browsers.

cbarilla’s picture

Is there a better solution...or am I just stuck because of the code?

nevets’s picture

What's the source of the script, what is it trying to do?

cbarilla’s picture

The script is just a mailling list signup form. The user enters some info in the form and it is validated in the script, then sent off to a remote server for storage and the message is generated. I am trying to hack the script to better integrate the messages into my site.

cbarilla’s picture

Well I figured out how to add classes to the code. Pretty simple once you learn a little jQuery.

Change this

mce_jQuery('#message').append(resp.msg);

to

mce_jQuery('#message').addClass('messages').addClass('status').append(resp.msg);