2016-04-07 11 views
1

enter image description hereenter image description hereJavaScript配列のPHP配列からget perticularフィールドを変換するには?

検索ボタンをクリックした後、携帯電話番号とヒットデータベースを取得します。結果がnullでない場合は、このfname、lname ....すべてのフィールドは、更新フォームのような塗りつぶしフォームで開きますが、結果が得られない場合、空白になります.... 検索ボタンに1つの関数を書き込みます携帯電話番号や追加出荷の..私の出荷コントローラにでajax.andを使用してメソッドを作成一つの方法は、私は携帯電話番号を取得し、結果チェック条件ごとに作成

public function actionCreate() 
    { 
     if(isset($_POST['mobilenumber'])) { 
    //$modelArray = Customers::find()->where(['cust_mobile' => $_POST['mobilenumber']])->one(); 
$connection = \Yii::$app->getDb(); 

$command = $connection->createCommand(
    "SELECT * ". 
    "FROM customers AS C ". 
    "WHERE C.cust_mobile= " .$_POST['mobilenumber'] 
      // "AND C.cust_mobile=" .$_POST['mobilenumber2'] 
);  
$modelArray = $command->queryAll(); 

if(count($modelArray) > 0) { 

print_r(json_encode($modelArray)); 
       /*Here I wanna set data which is comming from response i.e in object($modelArray) 
       on same form i.e is like a update form i.e here update logic i want to apply*/ 
      } else { 
       /*Here new record is create if enter mobile number is not exist*/ 
      } 

     } else { 
    /*open form if mobile number not set*/ 
     } 
    } 

を呼び出すフォーム(出荷/ _form.php ) 、出荷フォームでは、このFNAME、lanameすべての詳細は、出荷番号を顧客テーブルに保存したアドレスを、ボタンを作成

<script type="text/javascript"> 
     function searchResult() { 
    var mobilenumber = document.getElementById('customers-cust_mobile').value; 
    $.ajax({ 

    type: "POST", 
    data: { 
    mobilenumber: mobilenumber, 
    }, 
    dataType: "json", 
    url: "index.php?r=shipment/create", 
    success: function(data){ 
    $('#customers-cust_fname').val(data[0].cust_fname); 
    $('#customers-cust_mname').val(data[0].cust_mname); 
    $('#customers-cust_lname').val(data[0].cust_lname); 
    $('#customers-cust_email').val(data[0].cust_email); 
    $('#customers-cust_street').val(data[0].cust_street); 
    $('#customers-cust_address').val(data[0].cust_address); 

    } 
}); 
} 
    </script> 
    <?= $form->field($modelCustomersSender, 'cust_mobile')->textInput(['maxlength' => true]) ?> 
    <?= $form->field($modelCustomersSender, 'cust_fname')->textInput(['maxlength' => true]) ?> 
        <?= $form->field($modelCustomersSender, 'cust_mname')->textInput(['maxlength' => true]) ?> 
        <?= $form->field($modelCustomersSender, 'cust_lname')->textInput(['maxlength' => true]) ?> 

答えて

1

あなたのコードには、推奨されていないさまざまな方法があります。たとえば、適切なモデルを作成し、model-> loadで値をロードするか、またはビューのレンダリングを使用してデータを更新するためのajaxアクションを使用しない場合は、$ _POSTに直接アクセスするという事実です。すべての面が長すぎるため、私はあなたが書く方法に近いと思うような方法でコードを提供します。

あなたの行動は、あなただけが

myArray['your_attribute1'] = $_POST['your_attribute1']; 
.... 
myArray['your_attributeN'] = $_POST['your_attributeN']; 

$arrayToAjax= json_encode($myArray); 
echo $arrayToAjax; 

これはあなたのAJAXの成功の機能のためにJSON形式の結果を返す必要がありますすることができAJAXの値をレンダリングしたい場合は、そうでない場合は、この

public function actionCreate() 
{ 
    if(isset($_POST['mobilenumber'])) { 
     $modelArray = Customers::find()->where(['cust_mobile' => $_POST['mobilenumber']])->one(); 
     /*Here I got object in which whatever mobile number we enter 
     in text box. as per mobile number i got result and I check a condition is, if result count grater than 0 then 
     form open with fill text field else open with empty field */ 
     if(count($modelArray) > 0) { 
      $ 
      /*Here I wanna set data which is comming from response i.e in object($modelArray) 
      on same form i.e is like a update form i.e here update logic i want to apply*/ 
      $modelArray->attribute1 = $_POST['your_attribute1']; 
      ...... 
      $newModel->attributeN = $_POST['your_attributeN']; 
      $newModel->save() 
      // redirect or render what you prefer 
      return $this->redirect(['the_view_you_prefer']);    

     } else { 
      /*Here new record is create if enter mobile number is not exist*/ 
      $newModel = new Customers(); 
      $newModel->mobilenumber = $_POST['mobilenumber'] 
      $newModel->attribute1 = $_POST['your_attribute1']; 
      ...... 
      $newModel->attributeN = $_POST['your_attributeN']; 
      $newModel->save() 
      return $this->redirect(['the_view_you_prefer']); 
     } 

    } else { 
/*open form if mobile number not set*/ 
    } 
} 

などの代かもしれ作成します

成功のためにデータを取得するには、jsonParseを使用してください。

success: function(data){ 
     alert(data);//response display here 

     var jsonData = JSON.parse(data); 
     alert(jsonData[0].cust_fname); 
     alert(jsonData[0].cust_lname); 

     $('customers-cust_lname').val(data);//how to set value fot that text field from response 
     // window.location.reload(true); 
     } 
+0

ありがとうございました...私の作成部分は、更新部分のみがリームされていますので、私のif(count($ modelArray)> 0){...}条件はajaxレスポンスで正しく動作しますが、テキストフィールドでこの応答を設定することができません...私はajax関数を更新します –

+0

私はあなたのコメントを理解していない...私の答えはあなたが必要かどうかですか? – scaisEdge

+0

はい、私のために完全に答えの助けをしています –

関連する問題