I have created complex form, I'll try to provide simplified examples. There are ability to generate more fields by clicking +
button.
For example in form are fields:
Certificate Date Of Issue Date of Expire
[ ] [ ] [ ] +
by clicking +
button It add duplicate row (via javascript) so after clicking +
button part of form looks like:
NameOfVessel TypeOfVessel YearBuilt
[ ] [ ] [ ]
NameOfVessel TypeOfVessel YearBuilt
[ ] [ ] [ ] +
There are ability to click +
button as many times as user needs.
I have HTML form like this:
<li>
<ul class="column">
<li>
<label for="NameOfVessel">Name of Vessel</label>
<input id="NameOfVessel" type="text" name="NameOfVessel[]" class="field-style field-split25 align-left" placeholder="Name of Vessel" />
</li>
</ul>
</li>
<li>
<ul class="column">
<li>
<label for="TypeOfVessel">Type of Vessel</label>
<input id="TypeOfVessel" type="text" name="TypeOfVessel[]" class="field-style field-split25 align-left" placeholder="Type of Vessel" />
</li>
</ul>
</li>
<li>
<ul class="column">
<li>
<label for="YearBuilt">Year Built</label>
<input id="YearBuilt" type="text" name="YearBuilt[]" class="field-style field-split25 align-left" placeholder="Year Built" />
</li>
</ul>
</li>
PHP to insert to database. It should insert values from all added rows to multiple database table's rows, but for now it not inserting anything.
$UserID = get_current_user_id();
$NameOfVessel = mysqli_real_escape_string($link, $_POST['NameOfVessel']);
$TypeOfVessel = mysqli_real_escape_string($link, $_POST['TypeOfVessel']);
$YearBuilt = mysqli_real_escape_string($link, $_POST['YearBuilt']);
foreach($NameOfVessel as $key=>$res) {
$sql2 = "INSERT INTO CV_SeaServices (NameOfVessel, UserId, TypeOfVessel, YearBuilt) VALUES ('$res', '$UserId[$key]', '$TypeOfVessel[$key]', '$YearBuilt[$key]')";
if(mysqli_query($link, $sql2)){
echo "Resume created successfully.";
} else {
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
}
var_dump($NameOfVessel);
I've used var_dump
, but it returning NULL
. What's wrong with this code? Have you any ideas?
UPDATE
I've tried to do in following:
JS:
var noOfClicks = 0;
$(document).ready(function() {
$(".add-row").click(function() {
$("ul.sea-service").first().clone().appendTo(".personal-details1").append('<button class="remove">X</button>').find('input').val('');
noOfClicks += 1;
});
$("body").on('click', '.remove', function() {
$(this).closest('.sea-service').remove();
});
});
HTML:
<input id="NameOfVessel' + noOfClicks + '" type="text" name="NameOfVessel[]" class="field-style field-split25 align-left" placeholder="Name of Vessel" />
But in this case I got Id = ' + 'NameOfVessel' + noOfClicks + '
. As I understood I need to do that concatenation via javascript, just I can't achieve It correctly.