在 Laravel 控制器中获取 js 值


Get Js Value in Laravel Controller

如何在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");
  }
});

您应该使用 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");
  }
});