在动态生成输入字段时,数组中的POST数据存在问题。 我正在使用jquery进行生成,并想从ajax发送,我知道怎么做。
每次我发布数据时,数组中只有一个值。你能帮我吗? 非常感谢。
<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>
从jQuery生成多个字段的代码
(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);
用于将数据导入数据库时,我正在使用:
$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
});
});
答案 0 :(得分:0)
您在此部分代码中遇到问题:
class CustomerForm(forms.Form):
name = forms.CharField(max_length=50,required=True)
mobile = forms.CharField(max_length=50,required=True)
email = forms.EmailField(max_length=50,required=True)
class FamilyForm(forms.Form):
famname = forms.CharField(max_length=50,required=True)
address = forms.CharField(max_length=50,required=True)
<?php
$sql = mysqli_query($conn, "SQL Code");
while($row = mysqli_fetch_assoc($sql)){ ?>
<option value = "<?php echo ?>"><?php echo ?></option>
<?php } ?>
必须具有要在屏幕上打印的参数,因此请修复此部分,您应该可以提交。