У меня есть форма с повторяющейся секцией, которая использует jQuery для клонирования раздела и увеличения ид. Теперь мне нужно инициализировать переменные, чтобы отправить форму через PHP.
HTML:
<div class="repeatingSection">
<label for="poste_1">Poste :</label>
<input type="text" name="poste_1" id="poste_1"/>
<label for="date_1">Date :</label>
<input type="text" name="date_1" id="date_1"/>
</div>
Jquery:
jQuery('.cloneButton').click(function(event){
event.preventDefault();
var currentCount = jQuery('.repeatingSection').length;
var newCount = currentCount+1;
var lastRepeatingGroup = jQuery('.repeatingSection').last();
var newSection = lastRepeatingGroup.clone(false).find('.cloneButton').remove().end();
newSection.insertAfter(lastRepeatingGroup);
newSection.find("input").each(function (index, input) {
input.id = input.id.replace("_" + currentCount, "_" + newCount);
input.name = input.name.replace("_" + currentCount, "_" + newCount);
});
newSection.find("label").each(function (index, label) {
var l = jQuery(label);
l.attr('for', l.attr('for').replace("_" + currentCount, "_" + newCount));
});
return false;
});
jQuery(document.body).on('click','.removeButton', function(){
jQuery(this).closest('div.repeatingSection').remove();
return false;
});
PHP:
$poste1 = '';
$date1 = '';
$poste1 = trim($_POST['poste_1']);
$date1 = trim($_POST['date_1']);
Я знаю, что мне нужно будет поместить их в массив и пропустить их, но я не уверен, как это сделать.
Вы можете использовать цикл for и переменную count из javascript и сделать что-то вроде:
for($i=1; $i<$currentCount; $i++) {
${"poste".$i} = trim($_POST["poste_$i"]);
${"date".$i} = trim($_POST["date_$i"]);
}
У вас будут переменные $ poste1 и $ date1. Использование $ {''} создает динамические переменные.
$currentCount
, когдаvar currentCount
- это javascript. Я должен что-то упустить ... Или они не имеют ничего общего друг с другом, и мне нужно определить$currentCount
?