Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi all i have a small coding to copy the uploaded file path to a textbox using javascript. Its working fine in IE but onclick not firing in firefox.

HTML
<html>
<body>

<form method="post" action="<?php echo($_SERVER['PHP_SELF']);?>" name="form1" >
<label for="file">Filename:</label>
<input type="file" name="file1" id="file" ><br>
<input type="text" name="txtH" id="txtH"/>
<input type="button" name="submit" value="Submit" id="btnSubmit" onclick="fun()">

<script type="text/javascript">
function fun()
{
	document.getElementById('txtH').value=document.getElementById('file1').value;
	alert("hai");
}


</script>
</form>

</body>
</html> 
Posted
Comments
Peter_in_2780 3-Dec-12 2:43am    
Your element <input type="file" name="file1" id="file" > has an id of file, but your JS is looking for an id of file1
If that is really your code, then it shouldn't work in ANY browser.
So what is your code, really?

1 solution

You have error in this line...
JavaScript
document.getElementById('txtH').value = document.getElementById('file1').value;

You are accessing the File Uploader's path by getElementById('file1'), but the id is not 'file1', it is 'file' as per below...
XML
<input type="file" name="file1" id="file"></input>


Change the function to the below code, it will work.
JavaScript
function fun()
{
    document.getElementById('txtH').value=document.getElementById('file').value;
    alert("hai");
}
 
Share this answer
 
Comments
Member 9650694 3-Dec-12 2:01am    
yes thank you but i am not getting full path of the file only getting file name in firefox. How to solve this? Pls help
Ok let me work this out... I will come back with the reply.
Thanks for accepting the answer.
Actually you can't get the full file path as it is a security risk.
But this code gives the full path in IE browsers.
Check the below links for more details.
1. How to get full path from file upload box in javascript?
2. How to get the complete file path in Mozila

Thanks,
Tadit
Member 9650694 3-Dec-12 4:16am    
Thanks is there any other way or can we use any other control to get the full path?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900