【问题标题】:Laravel: Submit dynamically generated form fieldsLaravel:提交动态生成的表单字段
【发布时间】:2019-07-07 12:55:23
【问题描述】:

我正在尝试使用 Ajax 向控制器提交一个动态生成字段的表单。它就像一张发票。日期、客户、sub_total、tax 和 ttl 字段是永久性的(发票标题)。然后有 n 个动态生成的表单域(发票行)。这是动态生成的html。每个输入字段都有不同的名称和 ID。

<tbody id="list">
    <tr>
        <td>
            <input name="sku1" id="sku1" readonly="" type="text" value="K5693">
        </td>
        <td>
            <input name="name1" id="name1" readonly="" type="text" value="HANDLEBAR">
        </td>
        <td>
            <input name="rate1" id="rate1" readonly="" type="text" value="45.00">
        </td>
        <td>
            <input name="qty1" id="qty1" readonly="" type="text" value="2">
            </td>
        <td>
            <input name="disc1" id="disc1" readonly="" type="text" value="0">
        </td>
        <td>
            <input name="ttl1" id="ttl1" readonly="" type="text" value="90.00">
        </td>
        <td class="text-center">
            <a onclick="removeField(this)"><i class="fas fa-trash-alt"></i></a>
        </td>
    </tr>
    <tr>
        <td>
            <input name="sku2" id="sku2" readonly="" type="text" value="K5693">
        </td>
        <td>
            <input name="name2" id="name2" readonly="" type="text" value="HANDLEBAR">
        </td>
        <td>
            <input name="rate2" id="rate2" readonly="" type="text" value="45.00">
        </td>
        <td>
            <input name="qty2" id="qty2" readonly="" type="text" value="2">
        </td>
        <td>
            <input name="disc2" id="disc2" readonly="" type="text" value="10">
        </td>
        <td>
            <input name="ttl2" id="ttl2" readonly="" type="text" value="80.00">
        </td>
        <td class="text-center">
            <a onclick="removeField(this)"><i class="fas fa-trash-alt"></i></a>
        </td>
    </tr>
</tbody>

永久字段 HTML

        <div class="form-group">
            <label for="date">Date</label>
            <input type="text" class="form-control" id="date" name="date" placeholder="00/00/0000"></input>
        </div>
        <div class="form-group">
            <label for="customer">Customer</label>
            <select class="form-control" id="customer">
                @foreach($customers as $customer)
                    <option value="{{$customer->id}}">{{$customer->name}}</option>
                @endforeach
            </select>
        </div>

        <div class="card p-3">
            <table>
            <tr>
                <td class="h5">SUB TOTAL</td>
                <td class="h5 text-right pr-3">AED</td>
                <td><input type="text" class="form-control text-right" id="sub_total"placeholder="00.00" value="0.00" readonly></input></td>
            </tr>                   
            <tr>
                <td class="h5">VAT</td>
                <td class="h5 text-right pr-3">AED</td>
                <td><input type="text" class="form-control text-right" id="tax" placeholder="00.00" value="0.00" readonly></input></td>
            </tr>                       
            <tr>
                <td class="h5">TOTAL</td>
                <td class="h5 text-right pr-3">AED</td>
                <td><input type="text" class="form-control text-right" id="ttl" placeholder="00.00" value="0.00" readonly></input></td>
            </tr>
        </table>                        
    </div>

这里的javascript是通过单击按钮触发的:

function createData(){
    var date = document.getElementById('date').value;
    var customer = document.getElementById('customer').value;
    var sub_ttl = document.getElementById('sub_total').value;
    var tax = document.getElementById('tax').value;
    var ttl = document.getElementById('ttl').value;
}

我首先从永久字段中获取值。不确定如何从 n 个动态生成的字段中收集值并将其传递给控制器​​。已经浏览了许多关于此的文章,无法弄清楚。有什么帮助吗?

【问题讨论】:

    标签: javascript php jquery laravel


    【解决方案1】:

    如下更改您的代码。将&lt;form&gt;提交按钮放在你想要的位置。

    //submit opening invoice data
            $('#invoice_form').on('submit', function(event){
                event.preventDefault();
                $.ajax({
                    url:"put your url",
                    method:'post',
                    data:$(this).serialize(),
                    dataType:'json',
                    success:function(data)
                    {
                        //do whatever you want
                    }
                })
        });
    <form id ="invoice_form">
    <tbody id="body">
    <tr>
        <td>
            <input name="skus[]"readonly="" type="text" value="K5693">
        </td>
        <td>
            <input name="names[]" readonly="" type="text" value="HANDLEBAR">
        </td>
        <td>
            <input name="rates[]" readonly="" type="text" value="45.00">
        </td>
        <td>
            <input name="qtys[]" readonly="" type="text" value="2">
            </td>
        <td>
            <input name="discs[]" readonly="" type="text" value="0">
        </td>
        <td>
            <input name="ttls[]" readonly="" type="text" value="90.00">
        </td>
        <td class="text-center">
            <a onclick="removeField(this)"><i class="fas fa-trash-alt"></i></a>
        </td>
    </tr>
    </tbody>
    
      <button type="submit" class="btn btn-success"><i class="fas fa-download"></i> Save</button>
      
    </form>

    controller.php

     public function create(Request $request){
        if($request->ajax())
        {
         $skus= $request->skus;
         $names= $request->names;
         $rates= $request->rates;
         $qtys= $request->qtys;
    
         for($count = 0; $count < count($skus); $count++)
         {
          $data = array(
           'skus' => $skus[$count],
           'names'  => $names[$count],
           'rates'  => $rates[$count],
           'qtys'  => $qtys[$count]
          );
    
          $insert_data[] = $data; 
         }
    
         Invoice::insert($insert_data);
    
         return response()->json([
          'success'  => 'Invoice Saved Successfully!'
         ]);
        }
    }
    

    【讨论】:

      【解决方案2】:

      你将不得不这样做:

      <tbody id="body">
      <tr>
          <td>
              <input name="skus[]"readonly="" type="text" value="K5693">
          </td>
          <td>
              <input name="names[]" readonly="" type="text" value="HANDLEBAR">
          </td>
          <td>
              <input name="rates[]" readonly="" type="text" value="45.00">
          </td>
          <td>
              <input name="qtys[]" readonly="" type="text" value="2">
              </td>
          <td>
              <input name="discs[]" readonly="" type="text" value="0">
          </td>
          <td>
              <input name="ttls[]" readonly="" type="text" value="90.00">
          </td>
          <td class="text-center">
              <a onclick="removeField(this)"><i class="fas fa-trash-alt"></i></a>
          </td>
      </tr>
      

      然后在您的控制器中,您将能够像这样获得所有值:

      $skus = request('skus');
      $names = request('names');
      ...
      for($i = 0 ; $i < count($skus) ; $i++)
       {
         $sku = $skus[$i];
         $name = $names[$i];
         ...
       }
      

      【讨论】:

      • 不知道为什么这被否决了。这个方法有效。投票赞成。但我需要它使用 AJAX。不是标准的表单提交方式。
      • 抱歉我还没破解。我目前使用的是普通表单提交。
      • 嗨@JuanCarlosIbarra。我知道我看这个已经太晚了。但我想问一些事情。当我尝试您的答案时,错误出现在 Parse error: syntax error, unexpected 'i' (T_STRING), expecting ';' 上的消息 for(int i = 0 ; i &lt; count($skus) ; i++) 上。你能帮我解决这个错误吗
      • 我的错误@BariqDharmawan 我使用 Java 而不是 PHP 应该是这样的for ($i = 0 ; $i &lt; count($skus) ; $i ++)
      • 老实说,这是我在该主题上看到的最佳答案。这对我特别有用,因为我必须通过更新现有记录来做到这一点,而不是像互联网上的每个教程都想展示示例那样简单地创建新记录。只需在其中抛出一个 find() 函数并获取我需要更改的行然后使用此方法保存它很容易
      【解决方案3】:

      您真正需要的只是一种序列化整个表单的方法。

      以下是使用 jQuery 的方法:

      https://api.jquery.com/serialize/

      以下是使用纯 Javascript 的方法:

      Serialize HTML form to JSON with pure JavaScript

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-07
        • 1970-01-01
        • 1970-01-01
        • 2015-09-14
        相关资源
        最近更新 更多