从javascript获取输入值时无法将值传递给控制器

时间:2018-03-02 09:52:48

标签: javascript laravel laravel-5

我在Controller中获取所有其他输入数据并将其存储在数据库中,但我在输入字段中有总计,但我无法在Controller中获取该数据。 总计与其他输入字段之间的区别在于,我使用javascript获得总计。任何我出错的人,我认为我没有在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);
}

1 个答案:

答案 0 :(得分:2)

问题出在disabled关键字中,因为未提交已禁用的字段。

因此,您可以将输入设置为只读而不是禁用或添加隐藏输入,以便在提交时获取值:

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

隐藏输入:

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

不要忘记更新JavaScript代码中的两个字段:

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