samedi 12 mars 2016

Get Js Value in Laravel Controller

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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire