动态多输入字段,无法发送POST数据

I have problem with POST data from array in dynamically generating inputs fields. I am using jquery for generate and would like to send from ajax, i know how to do it probably.

Everytime when i POST data, i had only one value in array. Could you help me with that? Many thanks.

<div class = "col-md-4">
   <div class="form-group multiple-form-group input-group">
      <select class = "form-control" name = "cargos[]" required>
         <option value = "#" selected disabled>Wybierz ładunek</option>
         <?php
            $sql = mysqli_query($conn, "SQL Code");
            while($row = mysqli_fetch_assoc($sql)){ ?>
            <option value = "<?php echo ?>"><?php echo ?></option>
         <?php } ?>
      </select>
      <input type="number" name="values[]" class="form-control">
      <span class="input-group-btn">
         <button type="button" class="btn btn-success btn-add">+</button>
      </span>
   </div>
</div>

Code from jQuery to generating multiple fields

(function ($) {
$(function () {

    var addFormGroup = function (event) {
        event.preventDefault();

        var $formGroup = $(this).closest('.form-group');
        var $multipleFormGroup = $formGroup.closest('.multiple-form-group');
        var $formGroupClone = $formGroup.clone();

        $(this)
            .toggleClass('btn-success btn-add btn-danger btn-remove')
            .html('–');

        $formGroupClone.find('input').val('');
        $formGroupClone.find('.concept').text('Phone');
        $formGroupClone.insertAfter($formGroup);

        var $lastFormGroupLast = $multipleFormGroup.find('.form-group:last');
        if ($multipleFormGroup.data('max') <= countFormGroup($multipleFormGroup)) {
            $lastFormGroupLast.find('.btn-add').attr('disabled', true);
        }
    };

    var removeFormGroup = function (event) {
        event.preventDefault();

        var $formGroup = $(this).closest('.form-group');
        var $multipleFormGroup = $formGroup.closest('.multiple-form-group');

        var $lastFormGroupLast = $multipleFormGroup.find('.form-group:last');
        if ($multipleFormGroup.data('max') >= countFormGroup($multipleFormGroup)) {
            $lastFormGroupLast.find('.btn-add').attr('disabled', false);
        }

        $formGroup.remove();
    };

    var selectFormGroup = function (event) {
        event.preventDefault();

        var $selectGroup = $(this).closest('.input-group-select');
        var param = $(this).attr("href").replace("#","");
        var concept = $(this).text();

        $selectGroup.find('.concept').text(concept);
        $selectGroup.find('.input-group-select-val').val(param);

    }

    var countFormGroup = function ($form) {
        return $form.find('.form-group').length;
    };

    $(document).on('click', '.btn-add', addFormGroup);
    $(document).on('click', '.btn-remove', removeFormGroup);
    $(document).on('click', '.dropdown-menu a', selectFormGroup);

}); })(jQuery);

For importing data to database i am using:

$cargo = $_POST['cargo'];
$tonnages = $_POST['tonnages'];
      for($x = 0; $x < count($cargos); $x++) {
    mysqli_query($conn, "INSERT INTO cargos_specialOrder (specialOrder_ID, cargo_ID, tonnages) VALUES ('$index', '$cargos[$x]', '$tonnages[$x]')");
  }


    $("form").submit(function(e) {
  e.preventDefault();
  var formData = new FormData($("form")[0]);

  $.ajax({
    type: 'POST',
    url: 'php/newSpecialOrder.php',
    data: formData,
    success: function(response) {
      if(response == 'done') {
        $.confirm({
          title: 'Gotowe',
          content: 'Zgłoszenie zostało wysłane',
          type: 'green',
          typeAnimated: true,
          buttons: {
            close: function(){
              location.href = "index.php"
          }
        }
        });
      }
    },
    cache: false,
    contentType: false,
    processData: false
  });
});
doushun9875
doushun9875 补充说,谢谢你的帮助
接近 2 年之前 回复
dousui6193
dousui6193 你也可以发布ajax代码吗?所以我们可以跟踪它
接近 2 年之前 回复

1个回答

you have a problem in this section of the code:

<?php
            $sql = mysqli_query($conn, "SQL Code");
            while($row = mysqli_fetch_assoc($sql)){ ?>
            <option value = "<?php echo ?>"><?php echo ?></option>
         <?php } ?>

echo must have a parameter to be printed on screen, so fix this part and you should be able to submit.

douwen9343
douwen9343 你能告诉我怎么做吗? 我是初学用户,使用ajax,jquery
接近 2 年之前 回复
dongyixiu3110
dongyixiu3110 你能用ajax展示收到的表格样本吗?
接近 2 年之前 回复
doulun1666
doulun1666 我知道,我删除它,通常我有<?php $ sql = mysqli_query($ conn,“SELECT ID,CargoName From cargo ORDER BY CargoName”); while($ row = mysqli_fetch_assoc($ sql)){?> <option value =“<?php echo $ row ['ID']?>”> <?php echo $ row ['CargoName']?> </ option > <?php}?>
接近 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐