By cbarilla on
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
drupal_set_message() is part
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.
That makes sense. I didnt
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_cband this is the function
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.
The code puts the message
The code puts the message with
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.
I gathered that that is what
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.
Where ever there are lines
Where ever there are lines like
replace them with
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'.
Thanks for the help. I now
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.
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.
After some research I am
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?
Not jQuery code and there not
Not jQuery code and there not as portable between browsers.
Is there a better
Is there a better solution...or am I just stuck because of the code?
What's the source of the
What's the source of the script, what is it trying to do?
The script is just a mailling
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.
Well I figured out how to add
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);