注入 AJAX MooTools 响应

发布于 2024-12-04 01:08:27 字数 1394 浏览 1 评论 0原文

我有一个表单,用户将在其中输入文本,当他提交表单时,我认为必须注入我想要的文本,或者像 AJAX 注释一样简单地附加到页面,但我的问题是我可以在页面。

我已经测试了代码的工作(我已经警告了消息和其他内容)。

这是我的代码:

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_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();
  }
});

在 PHP 部分我有:

echo json_encode(array("result" => "success",
                       "message" => "Post message successful!",
                       "answer" => $content));

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_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));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千里故人稀 2024-12-11 01:08:27

如果responseObject.answer是您尝试放置在页面中的答案,这将帮助您开始。

将 onComplete 函数中的警报(responseObject.answer)替换为:

new Element('div': {
    html: responseObject.answer,
    class: 'optionalClassName'
}).inject($(document.body));

将 $(document.body) 替换为您想要的任何选择器例如:

$('comments')

或者

$('#comments .someOther > .selector')

您的帖子中提供的信息非常有限,所以如果我错过了标记,我深表歉意,只是尝试为您指明正确的方向。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文