Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

blank form field on false return ..

Status
Not open for further replies.

davef101

MIS
Mar 3, 2006
34
GB
can you help with this, i'm trying to blank the text field if the cancel button is selected ..

<script>
function uploadImage(thefield) {
input_box=confirm(" Upload / Replace image with this file ? ");
if (input_box==true) {
document.image.submit(); }
else {
thefield.value = "";
}
}
</script>
<body>
<form style="margin-top:0; margin-right:0; margin-left:0; margin-bottom:0;" name="image" method="post" enctype="multipart/form-data">

<font face="Verdana" size="2" color="#000000">Upload Product Image:<br>
<input type="file" name="fileName" size="23" value="" onChange="return uploadImage(this);">
</form>
</body>

I can't get the fileName field to default back to a 'blank' text box if the user clicks 'Cancel'.

Any help please, thanks
 
There I go..... 3 minutes again.

This time I have an alibi by finding a reference.

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
How about this then (still doesn't work, but you get what i'm trying to do!)

<script>
function uploadImage(thefield) {
input_box=confirm(" Upload / Replace image with this file ? ");
if (input_box==true) {
document.image.submit(); }
else {
var DefaultDivArray = new Array();
DefaultDivArray = "<font face=Verdana size=2 color=#000000>Upload Product Image:<br><input type=file name=fileName size=23 onChange='return uploadImage(this.value);'>";

if(document.all) {
document.all.UploadBox.innerHTML = DefaultDivArray;
}

else {
document.getElementById('UploadBox').innerHTML = DefaultDivArray;
}
}
}
}
</script>
<body>
<form style="margin-top:0; margin-right:0; margin-left:0; margin-bottom:0;" name="image" method="post" enctype="multipart/form-data">

<font face="Verdana" size="2" color="#000000">Upload Product Image:<br>
<div id="UploadBox">
<input type="file" name="fileName" size="23" value="" onChange="return uploadImage(this);">
</div>
</form>
</body>
 
Did you have a question hidden somewhere in that last post?

You can't change the contents of the input field, and from what it looks like you're trying to do it's not really necessary anyway. Whether or not the field clears after they decide they don't want to submit is not going to alter the functionality of the page in any way.

I see that you're not trying to change the contents of the field in your latest example, but rather trying to replace the field altogether. You found out it didn't work, and it's probably for the same reasons that it didn't work the first time. Did you want us to confirm that for you?

It's pretty much just accepted that type="file" inputs can't be dynamically altered. Instead of wasting time trying to break that system you might want to instead invest some time in cleaning up your code by getting rid of things like if (document.all) out of your application since it is completely unnecessary. Also, the <font> tag has been deprecated for the better part of this century - reading up on web standards and getting HTML that validates will be more worthy of your time rather than trying to hack something that functions the way it does for a reason.

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
Sorry to offend you kaht. I was simply offering a solution because i was told you can not change the field once submitted.

Yes, it is important on my site that the field clears if the user chooses not to upload the file, as i am using an 'onChange' instance .. anyway, i'll think twice before asking for advise on this site in the future, if i'm going to get a sacastic replys like yours.
 
My reply wasn't sacastic, it would benefit you to take my advice and learn web standards. It is unlikely that you will ever be asked by a potential employer if you know how to hack an input of type="file". However, it is very likely that you could be asked if you are up on web standards and cross browser programming. From the short snippet you posted it is apparent that you are not. My reply was not sarcastic, it was helpful. But if you do not choose to see it that way then it is your problem. Tek-tips is a great site where you can get good answers that are backed with years of experience. It just may not be guaranteed that the answer you get is what you want to hear, which again, is your problem.

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
You are correct though, clearing the field is necessary to submit the form because submission is triggered from an onchange of that field. However, providing automatic submission without a manual solution (via submit button) is another problem in and of itself.

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top