2017-03-27 2 views
0

こんにちは私の状況をheresので、私はlaravel 5.2フレームワーク上に構築された私のウェブサイトにストライプを統合しています。それは私が現在ストライプで作成したプランのIDを使用して、すべてのプランをドロップダウンして表示した場合、プランが選択されると、私はそれが自動的にフォームに記入したい計画の価格だけでなく、名前も付いています。私は現在私のストライプアカウント内のすべてのプランを取得するために私のコントローラでコールしています。その後、コレクションまたは配列を繰り返して、プランのIDを取得し、プランIDをビューにあるドロップダウンメニューに渡します。しかし、特定の計画が選択されたときに計画のための他の情報を得ることができるようにする必要があるので、私はフォームに自動的に記入することができます。正しくlaravelとデータベース呼び出しで配列を使用する

私はフォームを自動入力する準備ができています。私は、選択したプランから名前や価格など必要な値を取得する方法を知りません。私は配列を正しく使用していない気がします。どんな助けもありがとうございます。

管理コントローラ

try{ 
      $stripePlanIdsServer = Plan::all()->data; 
      $stripePlanIds = array(); 
      if($stripePlanIdsServer){ 
       foreach($stripePlanIdsServer as $stripePlanId){ 
        $stripePlanIds[] = $stripePlanId->id; 
       } 
      } 
     }catch (\Exception $e){ 
      return redirect()->route('users.administration.index')->with('error', $e->getMessage()); 
     } 

     return view('users.administration.index', compact('stripePlanIdsServer', 'stripePlanIds')); 

管理ビュー

<div class="row"> 
           <div class="col-sm-4 text-center"> 
            <div class="form-group label-floating"> 
             {!! Form::label('reoccurring', 'Reoccurring') !!} 
             </br> 
             {!! Form::select('reoccurring', ['Yes'=>'Yes','No'=>'No'], null, ['class' => 'browser-default mdl-selectfield', 'placeholder' => 'Choose Option', 'id' => 'reoccurring'])!!} 
            </div> 
           </div> 
           <div class="col-sm-4 text-center"> 
            <div id="planIdDiv" class="form-group label-floating" style="display: none;"> 
             {!! Form::label('planId', 'Plan ID (shows up on customers bank statement') !!} 
             </br> 
             {!! Form::select('planId', $stripePlanIds, null, ['class' => 'browser-default mdl-selectfield', 'placeholder' => 'Choose Option', 'id' => 'plan'])!!} 
            </div> 
           </div> 
           <div class="col-sm-2 text-center"> 
            <div id="billStatusDiv" class="form-group label-floating" style="display: none;"> 
             {!! Form::label('billStatus', 'Bill Status') !!} 
             </br> 
             {!! Form::select('billStatus', ['Not Paid'=>'Not Paid','Paid'=>'Paid'], null, ['class' => 'browser-default mdl-selectfield', 'placeholder' => 'Choose Option', 'id' => 'billStatus'])!!} 
            </div> 
           </div> 
           <div class="col-sm-2 text-center"> 
            <div id="organizationDiv" class="form-group label-floating" style="display: none;"> 
             {!! Form::label('organization', 'Assign Bill To') !!} 
             </br> 
             {!! Form::select('organization', $organization, null, ['class' => 'browser-default mdl-selectfield', 'placeholder' => 'Choose Option'])!!} 
            </div> 

           </div> 
          </div> 
          <div class="row"> 
           <div class="col-md-6"> 
            <div id="priceDiv" class="form-group label-floating" style="display: none;"> 
             <label class="control-label">Amount to Bill (in dollars)</label> 
             <input id="price" type="text" name="price" class="form-control"> 
            </div> 
           </div> 
           <div class="col-md-4"> 
            <div id="productNameDiv" class="form-group label-floating" style="display: none;"> 
             <label class="control-label">Product</label> 
             <input id="productName" type="text" name="productName" class="form-control"> 
            </div> 
           </div> 
          </div> 

答えて

1

良いことは、すでにあなたのビューにすべてのあなたの計画を渡しています。今すぐあなたのjavascriptに行き、

var plans = <php echo json_encode($stripePlanIdsServer);>; 

を行う。これは、すべてのあなたの計画のあるjavascriptの変数を行います。

次に選択し、あなたのPLANIDに、JavaScriptでのonchange

onchange="showDiv(this)" 

を追加し、定義しますshowDiv()関数

function showDiv(elem){ 
    plans.map(function(item) { 
     if (item.id == elem.value) { 
      //.value is the selected option value 
      //now do something to display the hidden div 
      document.getElementById('hidden_plan' + item.id).style.display = "block"; 
     } 
    }); 
} 

あなたの隠れた計画は、 ike this

<div id="hidden_plan1" style="display: none;">Plan 1</div> 
<div id="hidden_plan2" style="display: none;">Plan 2</div> 
<div id="hidden_plan3" style="display: none;">Plan 3</div> 

これは解決策ではありませんが、あなたはそのアイデアを持っています。最も重要なことは、プラン配列をPHPからJavascriptに変換し、Javascriptで使用できることです。

幸運を祈る!

+0

ありがとうございました!あなたが言及したように、私は少し修正を加えなければならなかったが、あなたは私が問題を解決するために必要な知識を私に与えて、今度は再び前進することができた!あなたの時間をありがとう! – rapid3642

関連する問題