2017-02-12 10 views
11

postパラメータをコントローラアクションにajax呼び出しで渡す際に問題が発生しました。他のajax呼び出しが意図したとおりに実行されるため、なぜそれが実行されているのかわかりません。次のように私は取得しています応答は次のとおりです。postパラメータが渡されていませんzend framework 2

Notice: Undefined index: user_bio in C:\xampp\htdocs\module\Members\src\Members\Controller\ProfileController.php on line 149 

コントローラコードは次のとおりです。

public function changebioAction() 
{ 
    $layout = $this->layout(); 
    $layout->setTerminal(true); 

    $view_model = new ViewModel(); 
    $view_model->setTerminal(true); 

    if ($this->request->isPost()) { 
     try { 
      $params = $this->params()->fromPost(); 

      $this->getProfileService()->editProfile(array('bio' => ltrim($params['user_bio']))); 
     } catch (ProfileException $e) { 
      $this->flashMessenger()->addErrorMessage($e->getMessage()); 

      return $this->redirect()->toRoute('members/profile', array('action' => 'change-failure')); 
     } 
    } 

    return $view_model; 
} 

JavaScriptとHTML:

// handle quick edit of bio 
function quickEditBio(element, button) { 
    var edited_data = $(element).html(); 

    var data = $(element).blur(function() { 
     edited_data = $(element).map(function() { 
      return this.innnerHTML; 
     }).get(); 
    }); 

    $(button).on('click', function() { 
     $.ajax({ 
      method : "POST", 
      url : "/members/profile/change-bio", 
      data : { 
       user_bio : edited_data[0] 
      } 
     }).done(function(msg) { 
      // bio saved 
      // go back to profile page 
      //location.href = '/members/profile'; 
      alert(msg); 
     }).fail(function() { 
      alert("Error changing bio, please try again."); 
     }); 
    }); 
} 

<button onclick="expand('bio')" class="w3-btn-block w3-theme-d2 w3-left-align"> 
       <i class="fa fa-book fa-fw w3-margin-right"></i> Bio 
      </button> 

      <div id="bio" class="w3-accordion-content w3-container"> 
       <div class="w3-display-container"> 
        <p id="bio-user" contenteditable="true"> 
         <?php echo $this->layout()->bio; ?> 
        </p> 

        <div class="w3-display-right"> 
         <button class="w3-btn-block w3-theme-d2" id="save-bio">Save</button> 
        </div> 

        <script type="text/javascript"> 
         quickEditBio('#bio-user[contenteditable=true]', $('#save-bio')); 
        </script> 
       </div> 
      </div> 

私はエラーが、前に述べたように索引user_bioが未定義であるということです。

Notice: Undefined index: user_bio in C:\xampp\htdocs\module\Members\src\Members\Controller\ProfileController.php on line 149 

本当に混乱している部分は、他の部分がこの同じコードを使用していて、うまく動作することです。

ご協力いただければ幸いです。

ありがとうございます!

答えて

5

JavaScriptに問題があります。

要素に「ぼかし」するたびに、edited_data変数が空の配列に設定されます。

edited_data[0]は未定義となり、あなたのPHPに掲載されます。

これは、user_bioがパラメータとして受信されていないため、$params['user_bio']が未定義のインデックスになることを意味します。

は、この問題を解決するには、次の
はもうできなくなります。またedited_dataこのようにすること edited_data[0]を変更する配列を $(element).mapを削除し、 $(element).html();
と交換してください。

+0

ありがとう、完璧に働いた。 – user2101411

関連する問題