2017-08-06 968 views
0

我試圖重新加載頁面後評論提交成功通過Ajax刷新或刷新網頁中的AJAX

提交該行

// Save the comment 
     $state = $comment->store(); 

如果我想以後 評論提交成功後評論使用location.reload();作爲

// Save the comment 
      $state = $comment->store(); 
location.reload(); 

那麼它不工作,發表評論甚至沒有提交 下面是完整的代碼 任何一個可以幫助 - 如何當註釋成功提交,並存儲在後端

<?php 
defined('_JEXEC') or die('Unauthorized Access'); 

require_once(JPATH_COMPONENT . '/views/views.php'); 

class EasyBlogViewComments extends EasyBlogView 
{ 
    /** 
    * Processes comment saving 
    * 
    * @since 4.0 
    * @access public 
    * @param string 
    * @return 
    */ 
    public function save() 
    { 
     // Check for request forgeries 
     EB::checkToken(); 

     // Test if user is really allowed to post comments 
     if (!$this->acl->get('allow_comment')) { 
      return $ajax->reject(JText::_('COM_EASYBLOG_NO_PERMISSION_TO_POST_COMMENT')); 
     } 

     // Default values 
     $moderated = false; 
     $parentId = $this->input->get('parentId', 0, 'int'); 
     $depth = $this->input->get('depth', 0, 'int'); 
     $subscribe = $this->input->get('subscribe', false, 'bool'); 
     $email = $this->input->get('email', '', 'email'); 
     $message = $this->input->get('comment', '', 'default'); 
     $name = $this->input->get('name', '', 'default'); 
     $username = $this->input->get('username', '', 'default'); 
     $password = $this->input->get('password', '', 'default'); 
     $title = $this->input->get('title', '', 'default'); 
     $terms = $this->input->get('terms', false, 'bool'); 
     $blogId = $this->input->get('blogId', 0, 'int'); 
     $isCB = $this->input->get('iscb', 0, 'int'); 

     // If there is no name, and the current user is logged in, use their name instead 
     if (!$name && $this->my->id) { 
      $user = EB::user($this->my->id); 
      $name = $user->getName(); 
     } 

     // Validate the email 
     $data = array('post_id' => $blogId, 'comment' => $message, 'title' => $title, 'email' => $email, 'name' => $name, 'username' => $username, 'terms' => $terms); 

     // Load up comment table 
     $comment = EB::table('Comment'); 
     $state = $comment->validatePost($data); 

     if (!$state) { 
      return $this->ajax->reject($comment->getError()); 
     } 

     // Bind the data on the comment table now 
     $comment->bindPost($data); 

     // Check for spams 
     if ($comment->isSpam()) { 
      return $this->ajax->reject(JText::_('COM_EASYBLOG_SPAM_DETECTED_IN_COMMENT')); 
     } 

     $captchaResponse = EB::captcha()->verify(); 

     // Perform captcha verification 
     if (isset($captchaResponse->success) && $captchaResponse->success == false) { 
      return $this->ajax->reject($captchaResponse->errorCodes); 
     } 

     // Get current date 
     $date = EB::date(); 

     // Set other attributes for the comment 
     $comment->created = $date->toSql(); 
     $comment->modified = $date->toSql(); 
     $comment->published = true; 
     $comment->parent_id = $parentId; 
     $comment->created_by = $this->my->id; 

     // Process user registrations via comment 
     $register = $this->input->get('register', '', 'bool'); 

     if ($register && $this->my->guest) { 

      if (empty($password) || empty($username) || empty($email)) { 
       return $this->ajax->reject('COM_EASYBLOG_COMMENT_REGISTRATION_FIELD_EMPTY'); 
      } 

      $userModel = EB::model('Users'); 
      $id = $userModel->createUser($username, $email, $name, $password); 

      if (!is_numeric($id)) { 
       return $this->ajax->reject($id); 
      } 

      $comment->created_by = $id; 
     } 

     $totalComments = $this->input->get('totalComment', 0, 'int'); 


     // Determines if comment moderation is enabled 
     if ($this->config->get('comment_moderatecomment') == 1 || ($this->my->guest && $this->config->get('comment_moderateguestcomment'))) { 
      $comment->published = EBLOG_COMMENT_STATUS_MODERATED; 
     } 

     // Load up the blog table 
     $blog = EB::table('Blog'); 
     $blog->load($comment->post_id); 

     // If moderation for author is disabled, ensure that the comment is also published automatically. 
     if ((!$this->config->get('comment_moderateauthorcomment') && $blog->created_by == $this->my->id) || EB::isSiteAdmin()) { 
      $comment->published = true; 
     } 

     // Update the ordering of the comment before storing 
     $comment->updateOrdering(); 

     // Save the comment 
     $state = $comment->store(); 


     if (!$state) { 
      return $this->ajax->reject($comment->getError()); 
     } 

     $resultMessage = JText::_('COM_EASYBLOG_COMMENTS_POSTED_SUCCESS'); 
     $resultState = 'success'; 



     // If user registered as well, display a proper message 
     if ($register) { 
      $resultMessage = JText::_('COM_EASYBLOG_COMMENTS_SUCCESS_AND_REGISTERED'); 
     } 

     if ($comment->isModerated()) { 
      $resultMessage = JText::_('COM_EASYBLOG_COMMENT_POSTED_UNDER_MODERATION'); 
      $resultState = 'info'; 
     } 

     // Process comment subscription 
     if ($subscribe && $this->config->get('main_subscription') && $blog->subscription) { 
      $subscribeModel = EB::model('Subscription'); 
      $subscribeModel->subscribe('blog', $blog->id, $email, $name, $this->my->id); 
     } 

     // Process comment notifications 
     $comment->processEmails($comment->isModerated(), $blog); 

     // Set the comment depth 
     $comment->depth = $this->input->get('depth', 0, 'int'); 

     // Update the sent flag 
     $comment->updateSent(); 

     // Format the comments 
     $result = EB::comment()->format(array($comment)); 
     $comment = $result[0]; 

     $language = JFactory::getLanguage(); 
     $rtl = $language->isRTL(); 

     $theme = EB::template(); 
     $theme->set('comment', $comment); 
     $theme->set('rtl', $rtl); 

     $output = ''; 

     if ($isCB) { 
      // if the is saving from CB plugin, then we need to display the output using different template. 
      $output = $theme->output('site/comments/cb.item'); 
     } else { 
      $output = $theme->output('site/comments/default.item'); 
     } 

     return $this->ajax->resolve($output, $resultMessage, $resultState); 
    } 
public function reloadCaptcha() 
{ 
    $ajax = EB::ajax(); 

    // Get the previous captcha id. 
    $id = $this->input->get('previousId', 0, 'int'); 

    $captcha = EB::table('Captcha'); 
    $state = $captcha->load($id); 

    if ($state) { 
     $captcha->delete(); 
    } 

    // Generate a new captcha 
    $captcha = EB::table('Captcha'); 
    $captcha->created = EB::date()->toSql(); 
    $captcha->store(); 

    $image = EB::_('index.php?option=com_easyblog&task=captcha.generate&tmpl=component&no_html=1&id=' . $captcha->id, false); 

    return $ajax->resolve($image, $captcha->id); 
} 

刷新和加載頁面第二個文件

EasyBlog.module('comments/form', function($) { 

    var module = this; 

    EasyBlog.require() 
    .script('comments/captcha', 'comments/list') 
    .library('markitup') 
    .done(function($) { 

     EasyBlog.Controller('Comments.Form', { 
      defaultOptions: { 

       "{formWrapper}": "[data-comment-form-wrapper]", 
       "{form}": "[data-comment-form]", 
       "{title}": "[data-comment-title]", 
       "{name}": "[data-comment-name]", 
       "{username}": "[data-comment-username]", 
       "{password}": "[data-comment-password]", 
       "{email}": "[data-comment-email]", 
       "{register}": "[data-comment-register]", 
       "{website}": "[data-comment-website]", 
       "{counter}": "[data-comment-counter]", 
       "{subscribe}" : "[data-comment-subscribe]", 
       "{terms}": "[data-comment-terms]", 
       "{tncLink}": "[data-comment-tnc]", 
       "{parentId}" : "[data-comment-parent-id]", 
       "{commentDepth}": "[data-comment-depth]", 
       "{blogId}" : "[data-comment-blog-id]", 
       "{depth}": "[data-comment-depth]", 
       "{notice}": "[data-comment-notice]", 
       "{editor}": "[data-comment-editor]", 
       "{submit}": "[data-comment-submit]", 
       "{formToken}": "[data-comment-token]", 

       "{recaptcha}": "[data-recaptcha-item]" 
      } 
     }, function(self, opts, base) { 

      return { 

       init: function() { 

        self.initEditor(); 

        self.list = self.addPlugin('list'); 

        // If recaptcha is enabled, we should skip the normal captcha 
        var recaptcha = self.recaptcha.inside(self.element).length; 

        if (recaptcha < 1) { 
         self.captcha = self.addPlugin('captcha'); 
        } 
       }, 

       initEditor: function() { 
        if (self.editor().data('comment-bbcode') == 1) { 
         self.editor().markItUp(window.EasyBlogBBCodeSettings); 
        } 
       }, 

       setNotice: function(message, type) { 
        var className = ''; 

        if (type == 'error') { 
         className = 'alert-danger'; 
        } 

        if (type == 'success') { 
         className = 'alert-success'; 
        } 

        if (type == 'info') { 
         className = 'alert-info'; 
        } 

        self.notice() 
         .removeClass('hide') 
         .addClass('alert ' + className) 
         .html(message); 
       }, 

       resetForm: function() { 
        // If the comment form has a parent id, we need to reposition the comment form back. 
        var parentId = self.parentId().val(); 

        if (parentId != 0) { 
         self.form().appendTo(self.formWrapper()); 
        } 

        // Reset the form 
        self.username().val(''); 
        self.password().val(''); 
        self.subscribe().attr('checked', false); 
        self.editor().val(''); 
        self.website().val(''); 
        self.name().val(''); 
        self.depth().val(0); 
        self.parentId().val(0); 

        self.trigger('resetForm'); 

        // Determine if recaptcha is available 
        var recaptcha = self.recaptcha.inside(self.element); 

        // Get recaptcha's response 
        if (recaptcha.length > 0) { 
         grecaptcha.reset(); 
        } 
       }, 

       resetNotice: function() { 
        self.notice() 
         .removeClass('info error') 
         .html(''); 
       }, 

       "{self} replyComment": function(el, event, commentItem, commentId, commentDepth) { 
        // Hide notices in the reply form 
        self.notice().addClass('hide'); 

        // When user tries to reply to an existing comment, move the form next to the level of the comment item 
        commentItem.after(self.form()); 

        self.depth().val(commentDepth); 

        // Set the new parent id to the comment's id 
        self.parentId().val(commentId); 
       }, 

       "{self} cancelReply": function(el, event, commentItem, commentId) { 
        // Set the parent id to 0 
        self.parentId().val(0); 

        // Reset the comment depth back to 0 
        self.depth().val(0); 

        // Relocate the form back to it's origin 
        self.formWrapper().html(self.form()); 
       }, 

       "{self} commentAdded": function() 
       { 
        // Increment the counter 
        var count = self.counter().html(); 
         count = parseInt(count) + 1; 

        self.counter().html(count.toString()); 
        self.resetForm(); 
       }, 

       getValues: function() { 

        var data = { 

         title: self.title().val(), 
         name: self.name().val(), 
         email: self.email().val(), 
         username: self.username().val(), 
         password: self.password().val(), 
         website: self.website().val(), 
         subscribe: self.subscribe().is(':checked') ? 1 : 0, 
         register: self.register().is(':checked') ? 1 : 0, 
         comment: self.editor().val(), 
         terms: self.terms().is(':checked') ? 1 : 0, 
         depth: self.depth().val(), 
         parentId: self.parentId().val(), 
         blogId: self.blogId().val() 
        }; 

        // token 
        // data[self.formToken().attr('name')] = 1; 

        // Determine if recaptcha is available 
        var recaptcha = self.recaptcha.inside(self.element); 

        // Get recaptcha's response 
        if (recaptcha.length > 0) { 
         data.recaptcha = grecaptcha.getResponse(); 
        } 

        self.trigger('submitComment', [data]); 
        return data; 
       }, 

       "{tncLink} click": function() { 
        EasyBlog.dialog({ 
         content: EasyBlog.ajax('site/views/comments/terms') 
        }) 
       }, 

       "{submit} click" : function(el, event) { 

        event.preventDefault(); 

        // Reset notices 
        self.resetNotice(); 

        // Add loading indicator on the button 
        $(el).attr('disabled', true); 

        var tmp = $(el).html(); 

        $(el).html('<i class="fa fa-repeat fa-spin"></i>'); 

        // Get the form values 
        var data = self.getValues(); 

        // Perform an ajax call to submit the comment 
        EasyBlog.ajax('site/views/comments/save', data) 
         .done(function(output, message, state) { 

          self.setNotice(message, state); 
          self.trigger('commentAdded',[output, data]); 




         }) 
         .fail(function(message) { 
          self.setNotice(message, 'error'); 
         }) 
         .always(function(){ 
          $(el).removeAttr('disabled'); 
          $(el).html(tmp); 

          self.trigger('reloadCaptcha'); 
         }); 

        return false; 
       } 
      } 
     }); 

     module.resolve(); 
    }); 
}); 
+0

https://stackoverflow.com/questions/31201838/reload-a-page-after-successfully-submit-a-form-using-ajax?rq=1 –

+0

使用ajax回調函數來執行'location.reload ()'? – RamRaider

+0

@RamRaider - 抱歉,但天真 - 如果可以建議在哪一行使用location.reload – Ruchika

回答

0

我會冒險猜測添加location.reload()的地方會在小號片段:

"{self} commentAdded": function() 
{ 
    // Increment the counter 
    var count = self.counter().html(); 
     count = parseInt(count) + 1; 

    self.counter().html(count.toString()); 
    self.resetForm(); 
}, 

因此,也許這樣的:

"{self} commentAdded": function() 
{ 
    // Increment the counter 
    var count = self.counter().html(); 
     count = parseInt(count) + 1; 

    self.counter().html(count.toString()); 
    self.resetForm(); 

    /* reload */ 
    location.reload(); 
}, 

或者,你可以內EasyBlog.ajax通過在代碼雖然看着它添加到.done方法....相當多,我認爲同樣的事情

+0

謝謝 - 嘗試添加location.reload();在上面和.done函數中,但不幸的是頁面沒有重新加載。雖然評論是在飛行中提交,但作爲ajax沒有頁面被刷新 – Ruchika

+0

:(嘗試把一個警報語句在任何位置,看看它是否彈出一條警告消息 - 就像測試 – RamRaider

+0

我的壞,甚至沒有警報聲明.. – Ruchika