Faiez
Faiez

Reputation: 305

Cannot pass value to controller while getting input value from javascript

I am getting all other input data in Controller and storing them in database but I have Total in input field but I am unable to get that data in Controller. Difference between Total and other input field is that, I am getting Total using javascript. Anyone where I am getting wrong, I think I am not giving correct name in Controller.

checkout.blade.php

<div class="row">
    {!! Form::open(['route'=>'address.store','method'=>'post'])!!}
        {{ csrf_field() }}

    <tr class="order-total">
    <th>Total</th>
        <td>
            <strong>
                <span class="woocommerce-Price-amount amount">
                <span class="woocommerce-Price-currencySymbol">PKR.
                  <input style="border: none; background-color: #FFFFFF" id="Total" name="Total" disabled title="Total"></span>
                </span>
            </strong>
        </td>
    </tr>
    {!! Form::close() !!}


<script>
      $(document).ready(function(){
          var span_Text = document.getElementById("SubT").innerHTML;
          var del1 = 150;
          var Total=+del1 + +span_Text;
          $("#delivery").html(del1);
          $("#note").html('Note: Delivery Charges will be Rs.150/-');
          $("#Total").val(Total);

          $('#location').change(function(){
              var x = ($(this).val());

              if(x === 'Other'){
                  if(span_Text >= 1500){
                  $('.formx').show();
                  var del2=200;
                  var Total1=+del2 + +span_Text;
                  $("#delivery").html(del2);
                  $("#note").html('Note: All Other Location Delivery Charges will be Rs.200/- and Order must of Rs.1500/-');
                  $("#Total").val(Total1);
                  }
                  else{
                  alert('Order must of Rs.1500/-');
                  $("#note").html('Note: All Other Location Delivery Charges will be Rs.250/- and Order must of Rs.1500/-');

                  }
              }
              else {
                  var Total2=+del1 + +span_Text;
                  $('.formx').hide();
                  $("#delivery").html(del1);
                  $("#note").html('Note: Delivery Charges will be Rs.150/-');
                  $("#Total").val(Total2);

              }
          });
      });
</script>

Controller.php

   public function store(Request $request)
    {
        $this->validate($request,[
            'fname'=>'required',
            'lname'=>'required',
            'Ttl'=>'required',
            'email'=>'required|email',
            'addressline'=>'required',
            'city'=>'required',
            'pro'=>'required',
            'phone'=>'required |regex:/^[0-9\-\+]{9,15}$/',
            'Total'=>'required'
//(0)[0-9]{9}
            //|regex:/^[0-9\-\+]{9,15}$/
        ]);
        Address::create($request->all());

        return redirect('/')->with('valPrev',1);
}

Upvotes: 2

Views: 589

Answers (1)

Maraboc
Maraboc

Reputation: 11083

The problem is in the disabled keyword, because disabled fields are not submitted.

So you can make the input read only instead of disabled or add a hidden input, to get value when submitting like this :

<span class="woocommerce-Price-currencySymbol">PKR.
      <input readonly style="border: none; background-color: #FFFFFF" id="Total" name="Total" title="Total"></span>
</span>

For the hidden input :

<span class="woocommerce-Price-currencySymbol">PKR.
      <input style="border: none; background-color: #FFFFFF" id="Total" disabled title="Total"></span>
      <input type="hidden" id="TotalHidden" name="Total" ></span>
</span>

And don't forget to update the two fields in the JavaScript code :

$("#Total").val(Total1);
$("#TotalHidden").val(Total1);
// and other places

Upvotes: 2

Related Questions