Classic ASP BasicsFor beginner programmers starting with "classic" ASP 3, pre-".NET." NOT for ASP.NET 1.0, 1.1, or 2.0
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Classic ASP Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
I have an asp page that consist of a user input form. On this form certain users are capable of filling in information and adding a new form (which will be added to the database that is linked to the webpage).
One of the fields for the form is a filename. The user will get this filename by pressing the "browse" button and searching for whatever file they want to include in their form. Well, if the user does not select a form when the press the "submit" button I receive an error message.
Error Type:
Microsoft VBScript runtime (0x800A0009)
Subscript out of range: '[number: -1]'
/wwwroot/testecp/postNew1.asp, line 264
To prevent this from happening when the user is running on the actual page I wanted to make a message box appear when the user clicks the "submit" button and the notify him/her that they need to select a file name by selecting the browse button. Once the user presses ok on this message box I DO NOT want the submit to still process. I want it to stay on the page so the user can select a filename. I have the messagebox appearing when user clicks submit and a filename had not been selected, however, it is still sending the user to the next page (hence still generating the subscript out of range error").
This same error also occurs when the user attempts to type the filename in him/herself and does so incorrectly. I tried making the filename box "readonly" but then that also sets the button to read only so you cannot select the file. I also tried setting the file's textbox onClick event to show a messagebox whenever the user clicked the textbox to attempt to type in it, however, it sets the "browse" button for the file textbox to that also. So even if they don't attempt to type the filename themselves the messagebox still appears when they click the "browse" button to select the file.
Just trying to keep the users from gettin' any kind of error message at all. Any suggestions? (Code posted below)
[...code...]
<script language = "VBScript">
<!--
Option Explicit
function checkData()
dim myForm
set myForm = Document.forms("newEcpForm")
if myForm.ecpFilename.Value = "" then
msgbox("You must click the browse button and select a file.")
return true
else
myForm.submit
end if
end function
I would actually use javascript for this (simply because I am more accustom to using it to validate client side and, to be 100% honest, have never used VBScript client side)
<script language=javascript>
<!--
function checkData(){
if (document.formname.ecpFilename.value.length == 0) {
alert("Please select a Form to work with!\n");
return false;
}
}
-->
</script>
in your form tag add something like this
<form name="myForm"> and the js line will become this:
document.myForm.ecpFilename.value.length == 0
*Remember* javascript is CASE SENSITIVE, so make sure you spell your form fields correctly!
hth.
--Stole this from a moderator
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.
***I most definitely think VBScript is easier, but I'm learning....
AnyWHO, it's not working. I've been at it for a couple of hours this morning and it's still doing the same thing. (No message box appears when I hit the submit button, just goes on to the "Page cannot be displayed" And it gives me this error: Error Type:
Microsoft VBScript runtime (0x800A0009)
Subscript out of range: '[number: -1]'
/wwwroot/testecp/postNew1.asp, line 271
Which is due to me not selecting a filename.
It shows a little error caution sign at the bottom left hand corner of the screen (when I press the back button). It says I have a syntax error on Line 19, Char 2, Code 0 (no clue what that means-- the code 0 part) on the PostNew1.asp page. This only occurs when I press the back button.
Now this is what I have....
PostNew1.asp
<%
if Session("UserID") = "" then
Response.Redirect("index1.asp")
end if
%>
<%
if Session("UserType") <> "originator" then
Response.Redirect("noPostNew1.asp")
end if
%>
<html>
<head>
<title>Post New ECP/IMI Impact Form</title>
<script language="javascript">
<!--
function checkData(){
if (document.myForm.ecpFilename.value.length !== 0 ) {
alert("Sending data...");
form.submit
return true;
}
}
else {
alert("missing file");
return false;
end if
}
}
end function
-->
</script>
</head>
[...code...]
'start of form--so u can see how it's name, etc.
<form name="myForm" method="post" action="postNew1.asp" enctype="multipart/form-data">
[...code...]
'The following is where the filename is located at on the form
[...code...]
'I posted the following code also because this is the reason for me getting the error when I do not select a filename... Thought maybe I need to be adding my script down there somewhere instead. ?
<%
set o = new clsUpload
if o.Exists("submitButton") then
'get client file name without path
sFileSplit = split(o.FileNameOf("ecpFilename"), "\")
sFile = sFileSplit(Ubound(sFileSplit))
quote:Originally posted by dparsons
I would actually use javascript for this (simply because I am more accustom to using it to validate client side and, to be 100% honest, have never used VBScript client side)
<script language=javascript>
<!--
function checkData(){
if (document.formname.ecpFilename.value.length == 0) {
alert("Please select a Form to work with!\n");
return false;
}
}
-->
</script>
in your form tag add something like this
<form name="myForm"> and the js line will become this:
document.myForm.ecpFilename.value.length == 0
*Remember* javascript is CASE SENSITIVE, so make sure you spell your form fields correctly!
hth.
--Stole this from a moderator
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.
First off, Javascript doesnt have "End Sub, Function, If" it uses { } to determine the begining and end of a code block.
Now, when you rewrote the javascript you had the logic backwards, becauces you were submitting the form when the file field was empty and not submiting the form when the file field had something in it.
The VBScript equiv to the above javascript is:
Dim s
s = Request.Form("ecpFilename")
If s = "" then
//alert message
else
//post form
end if
Also, you had: form.submit, this is a syntax error it needs to be:
myForm.submit();
lol I have no idea how it got changed so much. Trying to fix 2-3 things at once. lol Sorry. Okay, I'm going to go print the post out and try that (with javascript, mostly everywhere I searched does it in Javascript, so i'm going to try that first). Thanks a bunch ;)
Quote:
quote:Originally posted by dparsons
Oh wow, you totally destroyed that javascript code! O.o Its ok though. (Sorry I am a C# guy, I like javascript more ;] ) ANYWAY!
I took the liberty of editing your javascript and I threw together a quick HTML page so Ill explain it to you:
First off, Javascript doesnt have "End Sub, Function, If" it uses { } to determine the begining and end of a code block.
Now, when you rewrote the javascript you had the logic backwards, becauces you were submitting the form when the file field was empty and not submiting the form when the file field had something in it.
The VBScript equiv to the above javascript is:
Dim s
s = Request.Form("ecpFilename")
If s = "" then
//alert message
else
//post form
end if
Also, you had: form.submit, this is a syntax error it needs to be:
myForm.submit();
*PULLING HAIR OUT WHILE CURSING THE COMPUTER MONITOR*
Okay, I think I've calmed down now. This thing wants to drive me crazy today, but I've gotten it out of my system. Now, it is still doing the same thing, no msgbox, little caution sign, etc.
Maybe there's a more efficient and simpler way to do this. The way I see it, the message box isn't going to do me any good anyway because as long as it's not empty the program will work (with the javascript msgbox working). But it shouldn't allow the user to enter a filename at all. What would u suggest be the easiest way to make this happen?
I was thinking of throwing the whole msgbox thing out all together (I'll figure out how to work with that on another program) and maybe making the submit button readonly until the user selects a filename. I don't know, what do u think?
It's not required, but it just seems silly that they wouldn't have some sort of validation check to make sure the user selects a file, rather than typing one or leaving it blank. If they do either (type it in--it's very rare they'll get the entire file path right-- or leave blank), once they select the submit they're going to get that "Page cannot be displayed" page. They're not going to understand the "error message" so it seems only logical to add something to prevent this from occuring. It's almost like a crooked picture in a doctors office, it's just going to drive me crazy.
see like right here
'Upload filename
o.FileInputName = "ecpFilename" o.FileFullPath = Server.MapPath(".") & "\ecpFiles\" & sFile 'line270
o.save
Like here, maybe I need to be putting a validation check somewhere in here. if ecpFilename = "" then display a msgbox and return false? else it would continue on with o.FileFullPath = etc etc
Ok, take a deep breath, sit back and relax. Yes you are right, if i typed even the letter A into the file upload box, the javascript would allow me to pass and bomb your script; welcome to the fun world of error handling!
A word of warning before I continue on, using errors to control program flow is generally a bad idea (at the very least poor practice) unfortunately, sometimes it is the only way. =\ (Just dont make it a habit)
There is a bunch of things that you can do before an error can be thrown, examine the string, does if contain \ / or :? if it doesnt you know its not a valid path and that you should exit execution. Also I would leave the javascript in there as another measure of validation.
Lastly, on to the error checking. I am not going to actually write this out, 4Guys did a really great job of it and you can read the article here:
What you can examine is the type of error, Err.Description = "File not found" or something like that then you know that the user supplied an invalid file, exit program execution and warn them about it.
hth
p.s. check your yahoo mail before you leave work.
--Stole this from a moderator
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.
Not working still. No msgbox, my error handler isn't logging errors (or actually I think it is, I just don't have them coming out right--but that's the least of my worries right). I don't know what it is but this little caution box is irritating me and has led me to believe if I don't fix that, it's just not going to work.
soo once again here is the error message I continuously keep receiving once I refresh, go back, push submit, etc.