Reputation: 898
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:
Certificate Date Of Issue Date of Expire
[ ] [ ] [ ]
Certificate Date Of Issue Date of Expire
[ ] [ ] [ ] +
There are ability to click +
button as many times as user needs.
In database table CV_Certificates
I have columns CertificateId
, CertDateofIssue
, CertDateOfExpire
I'm using insert.php
to insert values to database in following:
$Certificate = mysqli_real_escape_string($link, $_POST['Certificate']);
$CertDateOfIssue = mysqli_real_escape_string($link, $_POST['CertDateOfIssue']);
$CertDateOfExpire = mysqli_real_escape_string($link, $_POST['CertDateOfExpire']);
$sql = "INSERT INTO CV_Certificates (Certificate, CertDateOfIssue, CertDateOfExpire) VALUES ('$Certificate','$CertDateOfIssue','$CertDateOfExpire')
if(mysqli_query($link, $sql)){
echo "Resume created successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
It inserting values only from 1 row, but It should insert values from all added rows to multiple database table's rows.
HTML
<fieldset class="fieldset-borders">
<legend>5. Certificates</legend>
<ul class="Certificates" id="Certificates">
<li>
<ul class="column">
<li>
<label for="Certificate">Certificate Name</label>
<input id="Certificate" type="text" name="Certificate" class="field-style field-split25 align-left" placeholder="Certificate" />
</li>
</ul>
</li>
<li>
<ul class="column">
<li>
<label for="CertDateOfIssue">Date of Issue</label>
<input id="CertDateOfIssue" type="date" name="CertDateOfIssue" class="field-style field-split25 align-left" placeholder="Date of Issue" />
</li>
</ul>
</li>
<li>
<ul class="column">
<li>
<label for="CertDateOfExpire">Date of Expire</label>
<input id="CertDateOfExpire" type="date" name="CertDateOfExpire" class="field-style field-split25 align-left" placeholder="Date of Expire" />
</li>
</ul>
</li>
</ul>
<ul class="Certificates1"></ul>
<button type="button" class="add-row1">+</button>
</fieldset>
Javascript
$(document).ready(function(){
$( ".add-row1" ).click(function(){
$( "ul.Certificates" ).first().clone().appendTo( ".Certificates1" ).append('<button class="remove">X</button>').find('input').val("");
});
$( "body" ).on('click', '.remove', function(){
$(this).closest('.Certificates').remove();
});
});
How could I achieve It? Also as I noticed, this is extremely simplified form, there are over 150 fields at all.
UPDATE
This is real part from my code which return an error: ERROR: Could not able to execute .
And not inserting values to database at all now.
foreach($SeaService as $key=>$res) {
$sql2 = "INSERT INTO CV_SeaServices (
UserId
,NameOfVessel
,TypeOfVessel
,YearBuilt
,Flag
,DWT
,TypeOfMEkW
,SSRankApplied
,SignOn
,SignOff
,CompanyName
) VALUES (
'$res',
'$NameOfVessel[$key]',
'$TypeOfVessel[$key]',
'$YearBuilt[$key]',
'$Flag[$key]',
'$DWT[$key]',
'$TypeOfMEkW[$key]',
'$SSRankApplied[$key]',
'$SignOn[$key]',
'$SignOff[$key]',
'$CompanyName[$key]')";
};
if(mysqli_query($link, $sql2)){
echo "Resume created successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
Upvotes: 0
Views: 1763
Reputation: 787
Each input addition must have a new name, or the variable must be an array:
<input id="Certificate" type="date" name="Certificate[]" class="field-style field-split25 align-left" placeholder="Date of Expire" />`
<input id="CertDateOfIssue" type="date" name="CertDateOfIssue[]" class="field-style field-split25 align-left" placeholder="Date of Issue" /> `
<input id="CertDateOfIssue" type="date" name="CertDateOfIssue[]" class="field-style field-split25 align-left" placeholder="Date of Issue" />`
Now in your PHP
$certificate = mysqli_real_escape_string($link, $_POST['Certificate']);
$certDateOfIssue = mysqli_real_escape_string($link, $_POST['CertDateOfIssue']);
$certDateOfExpire = mysqli_real_escape_string($link, $_POST['CertDateOfExpire']);
foreach($certificate as $key=>$res) {
$sql = "INSERT INTO CV_Certificates (Certificate, CertDateOfIssue, CertDateOfExpire) VALUES ('$res','$certDateOfIssue[$key]','$certDateOfExpire[$key]')
}
UPDATE
foreach($SeaService as $key=>$res) {
$sql2 = "INSERT INTO CV_SeaServices (
UserId
,NameOfVessel
,TypeOfVessel
,YearBuilt
,Flag
,DWT
,TypeOfMEkW
,SSRankApplied
,SignOn
,SignOff
,CompanyName
) VALUES (
'$res',
'$NameOfVessel[$key]',
'$TypeOfVessel[$key]',
'$YearBuilt[$key]',
'$Flag[$key]',
'$DWT[$key]',
'$TypeOfMEkW[$key]',
'$SSRankApplied[$key]',
'$SignOn[$key]',
'$SignOff[$key]',
'$CompanyName[$key]')";
if(mysqli_query($link, $sql2)){
echo "Resume created successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
}
Upvotes: 1