2016-04-07 20 views
1

我正在使用Yii2-advanced-app。我想將我選擇的下拉列表的值發送到同一頁面上的php代碼(顯示帶有複選框的列表)。請參看下面的圖片以供參考(這就是我真正想要的) -Yii2:發送數據到同一頁無法正常工作

Please see this image for reference

但是,選擇從下拉列表中的一些元素後,我得到的結果,但與整個HTML頁面中特定部分。這裏是捕捉參考 -

Here is the Snap for reference

我不明白爲什麼我得到了整個頁面響應。

我已經寫了下面的代碼 -

下拉列表

<?= $form->field($countries[0], 'id')->dropDownList(
    ArrayHelper::map($countries, 'phonecode', 'name'), 
    [ 
    'prompt' => 'Select Country', 
    'onchange' => ' 
    var id; 
    jQuery.ajax({ 
     type: "POST", 
     data: { 
      "id": $(this).val(), 
     }, 
     // url: "", 
     success: function(data){ 
      jQuery(".tobechanged").html(data); 
      alert(data); 
     } 
    });' 
    ])->label(false); 
    ?> 

和我各自的div是

<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 tobechanged" style="max-height: 400px; background: white; overflow: auto;"> 
    <ul class="list-unstyled" id="chkall"> 
     <?php 
     if(isset($contacts)) { 
      $contacts = $contacts; 
          // print_r($contacts[0]['cust_country_code']);exit(); 
      foreach ($contacts as $contact) { 
       if(isset($_POST['id'])) { 
        if($contact['cust_country_code'] == $_POST['id']) { 
         echo '<li> 
         <div class="checkbox" id="checkboxes"> 
          <label><input type="checkbox" value="'.$contact['cust_id'].'" id="'.$contact['cust_id'].'">'.$contact['cust_fname'].' '.$contact['cust_lname'].'</label> 
         </div> 
        </li>'; 
       } 
      } else { 
       echo '<li> 
       <div class="checkbox" id="checkboxes"> 
        <label><input type="checkbox" value="'.$contact['cust_id'].'" id="'.$contact['cust_id'].'">'.$contact['cust_fname'].' '.$contact['cust_lname'].'</label> 
       </div> 
      </li>'; 
     } 
    } 
} 
?> 
</ul> 
</div> 

它確實即使我們顯示「Hello」而不是複選框代碼,也不行。即這樣 -

<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 tobechanged" style="max-height: 400px; background: white; overflow: auto;"> 
    Hello 
</div> 

所以,請幫助我,以避免不必要的HTML。

我越來越喜歡這個

Response

回答

1

我將它以下列方式的細節 -

下拉 -

<?= $form->field($countries[0], 'phonecode')->dropDownList(
    ArrayHelper::map($countries, 'phonecode', 'name'), 
    [ 
    'prompt' => 'Select Country', 
    'onchange' => ' 
    dataType : "json", 
    $.post("index.php?r=messages/contactist&id="+$(this).val(), function(response) { 
     jQuery(".tobechanged").html(response); 
    });' 
    ])->label(false); 
    ?> 

股利'tobechanged'保持不變。這也沒關係,使其空,這樣的 -

<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 tobechanged">***Nothing Here***</div> 

然後,我展示我的清單中'contactist'方法如下 -

public function actionContactist($id) 
{ 
    $model = new Messages(); 
    $query = new \yii\db\Query; 
    $query->select(['cust_id','cust_fname','cust_lname','cust_mobile'])->from('customers')->where(['cust_country_code' => $id])->orderBy('cust_fname'); 
    $query->createCommand(); 

    $contactList = new ActiveDataProvider([ 
    'query' => $query, 
    'pagination' => false, 
    ]); 
    $contacts = $contactList->getModels(); 
    // print_r(json_encode($contacts)); 
    echo '<ul class="list-unstyled" id="chkall">'; 
    foreach ($contacts as $contact) { 
    echo '<li> 
    <div class="checkbox" id="checkboxes"> 
     <label><input type="checkbox" class="case" name="case" value="'.$contact['cust_id'].'" id="'.$contact['cust_id'].'">'.$contact['cust_fname'].' '.$contact['cust_lname'].'</label> 
     <input type="hidden" value="'.$contact['cust_mobile'].'" id="'.$contact['cust_id'].'"> 
    </div> 
</li>'; 
} 
echo '</ul>'; 
} 

就是這樣。

0

,你可以在你的控制器動作使用renderPartial爲Ajax調用的響應。

here