2016-03-12 161 views
0

如何在Laravel控制器中獲取js值。我想將隱藏字段的值傳遞給數據庫,但令我驚訝的是,它爲這些字段保存了空記錄。當我在控制檯上記錄這些值時,我看到它,但它不作爲值傳遞給控制器​​。我有什麼更好的辦法來處理這個問題?在Laravel控制器中獲取Js值

public function store(UserContactRequest $userContactRequest) 
    { 
     $phone   = $userContactRequest->phone_output; 
     if(Auth::user()) { 
      if($userContactRequest->isMethod('post')) { 
       $userContact = UserContact::firstOrNew(array('user_id'=>Auth::user()->id)); 
       $userContact->phone  = $phone; 
       $userContact->save(); 
       return redirect()->route('userContactIndex')->with('message', 'Your question has been posted.'); 
      }else{ 
       return redirect('user/contact/create')->withErrors($userContactRequest)->withInput(); 
      } 
     } 
    } 

這是刀片文件

<TH>FIELD</TH><TH>DECRIPTION</TH> 
        <input type="hidden" name="phone_output" id="phone_output"> 
        <TR><TD><div>{!! Form::tel('phone', Input::old('phone'), ['class'=>'mid first-input-div', 'placeholder'=>'8023472436', 'id'=>'phone']) !!}</div></TD><TD class="font-description"><SPAN id="errNm1"><STRONG><I>FIRSTNAME</I></STRONG></SPAN> field requests the input of your surname or family name. Only <SPAN><STRONG><I>alphabets</I></STRONG></SPAN> are accepted as valid inputs. </TD></TR> 

這是js文件:

$("#phone").intlTelInput(); 

var input = $("#phone"), 
    output = $("#phone_output"); 

input.intlTelInput({ 
    nationalMode: true, 
    utilsScript: "http://localhost:88/build/js/utils.js" // just for formatting/placeholders etc 
}); 

// listen to "keyup", but also "change" to update when the user selects a country 
input.on("keyup change", function() { 
    var intlNumber = input.intlTelInput("getNumber"); 
    if (intlNumber) { 
    output.text("International: " + intlNumber); 
    console.log(intlNumber); 
    } else { 
    output.text("Please enter a number below"); 
    } 
}); 

回答

1

您應該使用output.val()設置爲隱藏的輸入字段的值。 您已使用output.text()代替。

更新片段:

input.on("keyup change", function() { 
    var intlNumber = input.intlTelInput("getNumber"); 
    if (intlNumber) { 
    output.val("International: " + intlNumber); 
    console.log(intlNumber); 
    } else { 
    output.val("Please enter a number below"); 
    } 
});