I have created a simple PHP form that appends a field by calling a javascript function, every time a user clicks on a "Add" button.
The form code:
<form name="academicForm" method="post" action="send.php">
<script src="addInput.js" language="Javascript" type="text/javascript"></script>
<tr>
<td align="center" valign="middle">
<div id="dynamicInput">
<span id="dynamicInput">Entry #1</span><br />
<input type="text" name="myInputs[]"/>
<select name="formGender[]">
<option value="">Choose one...</option>
<option value="First">first</option>
<option value="Second">second</option>
<option value="Third">third</option>
</select> <br>
<input type="submit" value="Submit">
<input type="button" value="Add fields" onclick="addInput('dynamicInput');"/>
</div>
</td>
</tr>
</table>
</form>
The javascript code:
var counter = 1;
function addInput(divName){
var newdiv = document.createElement('div');
newdiv.innerHTML = "Entry #" + (counter + 1) + "<br><input type='text' name='myInputs[]'> <select name='formGender[]'> <option value=''>Chose one...</option><option value='First'>first</option><option value='Second'>second</option><option value='Third'>third</option></select>";
newdiv.setAttribute('id', divName);
document.getElementById(divName).appendChild(newdiv);
counter++;
}
}
Then, when one press the Submit button is directed to the send.php where the $_POST variables used in the form are to be parsed:
$myInputs = $_POST["myInputs"];
$formGender = $_POST["formGender"];
foreach ($myInputs as $eachInput) {
$output .= $eachInput . "\n";
}
foreach ($formGender as $eachInput2) {
$output .= $eachInput2 . "\n";
}
Then, anytime i run this, only the results of the first form entries are printed. All the data inserted in the form fields generated by the Javascript, despite the fact that are directed to the same variables, are not there.
Can anyone help me on this?
Update
Both the Javascript and PHP code have been changed in order to solve the Repeated ID's and the "add" position:
Javascript:
newdiv.setAttribute('id', divName+counter);
document.getElementById(divName).appendChild(newdiv);
PHP:
<form name="academicForm" method="post" action="send.php">
<script src="addInput.js" language="Javascript" type="text/javascript"></script>
<tr>
<td align="center" valign="middle">
<div id="dynamicInputForm">
<input type="button" value="Add fields" onclick="addInput('dynamicInput');"/>
<span id="dynamicInput">Entry #1</span><br />
<input type="text" name="myInputs[]"/>
<select name="formGender[]">
<option value="">Choose one...</option>
<option value="First">first</option>
<option value="Second">second</option>
<option value="Third">third</option>
</select> <br>
<input type="submit" value="Submit">
</div>
</td>
</tr>
</table>
</form>
And here's the result of adding 3 new elements to the form, as seen in Chrome Inspector:
<div id="dynamicInputForm">
<input type="button" value="Add" onclick="addInput('dynamicInputForm');">
<br>
<br>
<span id="dynamicInput">Entry #1</span><br>
<input type="text" name="myInputs[]">
<select name="formGender[]">
<option value="">Choose one...</option>
<option value="First">first</option>
<option value="Second">second</option>
<option value="Third">third</option>
</select>
</select> <br>
<div id="dynamicInputForm1"> /*HTML code above*/ </div>
<div id="dynamicInputForm2"> /*HTML code above*/</div>
<div id="dynamicInputForm3">/*HTML code above*/</div>
</div>
<input type="submit" value="Submit">
The result of print_r($_POST);
Array ([myInputs] => Array ( [0] => firstEntry ) [formGender] => Array ( [0] => Third )
Thanks in advance!