I have a form with a hidden object:

<input id="tracker" type="hidden" name="tracker" value="0"  />

The value is read with JavaScript and increased each time an element is added:

var tracker = document.getElementById("tracker");
var tracked = parseFloat(tracker.value) + 1;
tracker.value = tracked;

This works fine while in HTML and JavaScript, but when I submit the form to PHP the number gets all screwed up:

<form id="uploadForm" action="uploadHandler.php" enctype="multipart/form-data" method="post">
<input id="tracker" type="hidden" name="tracker" value="4"  />
$count = $_POST['tracker'];
echo $count;
// 401234

So lets say if the value in the form is 4, then when its echoed from PHP its value is 401234.
Why is it doing this?

Just html with a starting value of 0.

<form action="uploadHandler.php" enctype="multipart/form-data" method="post" id="uploadForm">
<input type="hidden" name="tracker" value="0" id="tracker" />
<div id="elementContainer"></div>
<input type="button" value="Add Another Image" onclick="addElement();" />
<input type="submit" value="Start Upload" name="submit" onclick="uploadProgress();" /> <input type="button" value="Reset" onclick="resetElements();"/>
</form>

I've actually worked around this, but I'm still curious why this happens.
I didn't realize that if you name the file input fields like this (name='uploadFile[]') it basically creates an array when passed to php. (In this case I'm adding them with JavaScript)

newdiv.innerHTML = "<input type='file' name='uploadFile[]' />  <input type='button' value='Remove' onclick='removeElement("+divId+");' />"

So basically you can just iterate through the array in php to move the files.

This article has been dead for over six months. Start a new discussion instead.