0

How do I get the js value in Laravel controller. I want to pass the value from the hidden fields to be saved in the db, but to my surprise it saves empty records for these fields. When I logged the values on the console, I am seeing it but it is not passed as the value to the controller. What better way am I to handle this?

 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();
            }
         }
    }

This is the blade file

<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>

THis is the js file:

$("#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 Answer 1

1

You should use output.val() to set a value for the hidden input field. You have used output.text() instead.

Updated snippet :

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");
  }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.