2016-08-05 72 views
1

我試圖在選擇dropdownlist(customerid)時自動填充文本框(Customername,CustomerAddress,customerMobileno)。客戶ID被選擇成功,但出現錯誤控制器操作被取回。我得到了404錯誤。我想提一下,我在這裏告訴我使用的很漂亮 - Yii2. Access to higher level folder404 not found in dependent textbox in select2 widget in yii2

在Firebug我得到我的選擇2插件以下無錯誤

GET http://localhost:8080/amitopticals/debug/default/toolbar?tag=57a4b1623a201 

200 OK 
     90ms  
create (line 545) 
GET http://localhost:8080/amitopticals/orders/orders/....php?r=orders/customer/get-for-customer&custid=2 

404 Not Found 
     10ms  
jquery.js (line 9175) 
ParamsHeadersResponseHTMLCookies 

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> 
<head> 
<title>Object not found!</title> 
<link rev="made" href="mailto:[email protected]" /> 
<style type="text/css"><!--/*--><![CDATA[/*><!--*/ 
    body { color: #000000; background-color: #FFFFFF; } 
    a:link { color: #0000CC; } 
    p, address {margin-left: 3em;} 
    span {font-size: smaller;} 
/*]]>*/--></style> 
</head> 

<body> 
<h1>Object not found!</h1> 
<p> 


    The requested URL was not found on this server. 



    The link on the 
    <a href="http://localhost:8080/amitopticals/orders/orders/create">referring 
    page</a> seems to be wrong or outdated. Please inform the author of 
    <a href="http://localhost:8080/amitopticals/orders/orders/create">that page</a> 
    about the error. 



</p> 
<p> 
If you think this is a server error, please contact 
the <a href="mailto:[email protected]">webmaster</a>. 

</p> 

<h2>Error 404</h2> 
<address> 
    <a href="/">localhost</a><br /> 
    <span>Apache/2.4.16 (Win32) OpenSSL/1.0.1p PHP/5.6.12</span> 
</address> 
</body> 
</html> 

代碼和相關的JavaScript -

<?= $form->field($model, 'o_customerid')->widget(Select2::classname(), [ 
        'data' => ArrayHelper::map(Customer::find()->all(),'c_id','customerDetails'), 
        'language' => 'en', 
        'options' => ['placeholder' => 'Select Customer Details', 'id' => 'custid'], 
        'pluginOptions' => [ 
         'allowClear' => true 
        ], 
        ]); 
       ?> 
<?php 
$script = <<< JS 
$('#custid').change(function(){ 
    var custid = $(this).val(); 

    $.get('index.php?r=orders/customer/get-for-customer',{ custid : custid }, function(data){ 
     alert(data); 
     var data = $.parseJSON(data); 
     $('#orders-o_customername').attr('value',data.c_name); 
     $('#orders-o_customeraddress').attr('value',data.c_address); 
     $('#orders-o_customermobno').attr('value',data.c_mobileno); 
    }); 
}); 
JS; 
$this->registerJs($script); 
?> 

控制器動作中CustomerController -

public function actionGetForCustomer($custid) 
    { 
     $customer = Customer::find()->where(['c_id'=>$custid])->asArray()->one(); 
     echo Json::encode($customer); 
    } 

回答

1

如果你使用漂亮的網址你的結果網址應該是

://localhost:8080/amitopticals/orders/customer/get-for-customer?custid=2 

,而不是導致你郵件標題

://localhost:8080/amitopticals/orders/orders/....php?r=orders/customer/get-for-customer&custid=2 

這樣對你$得到的,而不是

$.get('index.php?r=orders/customer/get-for-customer',{ custid : custid }, function(data){ 
    alert(data); 
    ........ 

您應該使用

$.get('customer/get-for-customer',{ custid : custid }, function(data){ 
    alert(data); 
+0

嗨scaisEdge,控制器動作在CustomerController中。但'客戶/獲取客戶'並沒有工作'/客戶/獲取客戶'。然後,我將控制器操作移到了OrdersController,並嘗試了「爲客戶獲取」,並且工作。但是,我怎樣才能使用其他控制器(在這種情況下CustomerController)? – Tanmay

+0

訂單模塊中有2個控制器 - OrdersController和CustomerController。 – Tanmay

+0

在customerController中,您可以使用Url :: to('[Orders/get-for-customer']);使用UrlHelpers,你必須添加'使用yii \ helpers \ Url;' – scaisEdge