I'm using a feature from formmail.php which allows me to use a hidden input that will rename a file uploaded by the user. This way I can keep the value of the real company name while removing illegal characters from the composed file name.
I've written a function that would do the proper replacements and assign the new value to a string variable, so I could use at the hidden input. Like this:
<input type="text" id="CompanyName" name="CompanyName" size="60" maxlength="40" onchange="clean(this.value)" required>
<script type="text/javascript">
cleanName = ""; // variable outside the function meant to be Global
function clean()
{
cleanName = document.getElementById("CompanyName").value.replace(/[/\\/'"*&%$]/g, "");
}
</script>
Nevertheless, when I try to use that "cleanName" variable, its value appears empty:
<input type="hidden" name="file_names" value="Proof=State.%2D%.cleanName.%'-Paymnt'%.%2D%.Proof" />
The file is uploaded as "NY--Paymnt-FileName.pdf".
How can I make this work?
You have just put the literal
cleanNamein an attribute value, not the value of the variable carrying that name.You could write to the
valueattribute of the hidden input.For that you should give your hidden input an
id, likeid=file_names(same asname).Then change the script to this (forget the global variable):
The HTML for the hidden input can go without a value:
NB: I have no idea what you are going to do with that value; it looks strange, but I expect you know what you are doing on the server side.