Inject AJAX MooTools response
I have a form where the user will input a text and when he submits the form, the text I want must be injected, I think, or simply attached to the page like AJAX comments, but my problem is that I can get the message on the page.
I have the code working tested (I have alert the message and other stuff).
Here's my code:
en4.questionanswer = {
postAnswer : function() {
var mess = $('a_mess');
var user_id = $('q_user_id');
var question_id = $('question_id');
//check if value is blank
//post Question
var request = new Request.JSON({
'method' : 'post',
'url' : en4.core.baseUrl + 'question/postans',
'data' : {
'mess' : mess.value,
'question_开发者_StackOverflow中文版id' : question_id.value
},
'onComplete':function(responseObject) {
if( typeof(responseObject)!="object") {
//alert('ERR');
}
else {
if (responseObject.result == "success") {
mess.value=''; //clear text after submiting
// en4.questionanswer.start("1");
alert(responseObject.answer);
}
else {
alert(responseObject.message);
}
}
}
});
request.send();
}
});
In the PHP part I have:
echo json_encode(array("result" => "success",
"message" => "Post message successful!",
"answer" => $content));
If responseObject.answer is the answer you are trying to place in the page this will get you started.
replace the alert(responseObject.answer) in the onComplete function with:
new Element('div': {
html: responseObject.answer,
class: 'optionalClassName'
}).inject($(document.body));
Replace $(document.body) with any selector you would like example:
$('comments')
or
$$('#comments .someOther > .selector')
The information provided in your post was very limited so if I missed the mark I apologize, just trying to point you in the right direction.
精彩评论