The file fields weren't getting passed through POST because originally we were adding the code after we closed the form.
When you use javascript to add fields, it doesn't update the source code.
In the head tag, you have to add name='' to the input tag. What I did to get the server to take the data with POST, was i named each field the same thing and created an array and then used a for each to loop through the array and print all the values of the array but you can change the code in the for each to whatever you want to do with the info.
HTML Code:
<html>
<head>
<title></title>
<script language="javascript">
fields = 0;
function addInput() {
if (fields != 10) {
document.getElementById('text').innerHTML += "<input type='file' value='' name='field[]' /><br />";
fields += 1;
} else {
document.getElementById('text').innerHTML += "<br />Only 10 upload fields allowed.";
document.form.add.disabled=true;
}
}
</script>
</head>
<body>
<form name="form" action="form.php" method="post">
<input type="button" onclick="addInput()" name="add" value="Add input field" />
<div id="text">
</div>
<br />
<input type="submit" value="Submit" />
</form>
</body>
</html>
PHP code:
HTML Code:
<html>
<head>
<title></title>
</head>
<body>
<?php
foreach($_POST['field'] as $value) {
echo "$value <br />"; // change this to what you want to do with the data
}
?>
</body>
</html>
Note: when you add a new field, it erases the text from all the other fields.