computer1000
asked on
See the code here
I am trying to submit the form to the particular asp page. If the customer uploads image that ends in .jpg, it should take to newphoto.asp
<form method="POST" action="newphoto.asp" enctype="multipart/form-da ta" id=form1 name=form1>
. Otherwise, the customer gets the error.
The below code is fine. But, when I tested upload an image that ends in .gif, I clicked on the 'Upload a Photo' button. It's nothing and does not take me to the asp page..
How to fix it? I have only 5 points right now. But, I promise you that you will get 50 or more points for solving this.
<SCRIPT language="JavaScript">
<!-- Hide script from older browsers
extArray = new Array(".gif", ".jpg", ".png");
function LimitAttach(form, file) {
allowSubmit = false;
if (!file) return;
while (file.indexOf("\\") != -1)
file = file.slice(file.indexOf("\ \") + 1);
ext = file.slice(file.indexOf(". ")).toLowe rCase();
for (var i = 0; i < extArray.length; i++) {
if (extArray[i] == ext) { allowSubmit = true; break; }
}
if (allowSubmit) form.submit();
else
alert("Please only upload files that end in types: "
+ (extArray.join(" ")) + "\nPlease select a new "
+ "file to upload and submit again.");
}
// End -->
</script>
<body>
Click "Browse" to choose the photo from your computer.<BR>
Please upload only images that end in:
<script> document.write(extArray.jo in(" "));
</script><BR>
Then click "Upload Your Photo" to upload it
<form method="POST" action="newphoto.asp" enctype="multipart/form-da ta" id=form1 name=form1>
<input type="file" name="UploadForm" size="30"><BR><BR>
<input type="button" name="submit" value="Upload Your Photo!" onclick="LimitAttach(this. form, this.form.UploadForm.value )">
<form method="POST" action="newphoto.asp" enctype="multipart/form-da
. Otherwise, the customer gets the error.
The below code is fine. But, when I tested upload an image that ends in .gif, I clicked on the 'Upload a Photo' button. It's nothing and does not take me to the asp page..
How to fix it? I have only 5 points right now. But, I promise you that you will get 50 or more points for solving this.
<SCRIPT language="JavaScript">
<!-- Hide script from older browsers
extArray = new Array(".gif", ".jpg", ".png");
function LimitAttach(form, file) {
allowSubmit = false;
if (!file) return;
while (file.indexOf("\\") != -1)
file = file.slice(file.indexOf("\
ext = file.slice(file.indexOf(".
for (var i = 0; i < extArray.length; i++) {
if (extArray[i] == ext) { allowSubmit = true; break; }
}
if (allowSubmit) form.submit();
else
alert("Please only upload files that end in types: "
+ (extArray.join(" ")) + "\nPlease select a new "
+ "file to upload and submit again.");
}
// End -->
</script>
<body>
Click "Browse" to choose the photo from your computer.<BR>
Please upload only images that end in:
<script> document.write(extArray.jo
</script><BR>
Then click "Upload Your Photo" to upload it
<form method="POST" action="newphoto.asp" enctype="multipart/form-da
<input type="file" name="UploadForm" size="30"><BR><BR>
<input type="button" name="submit" value="Upload Your Photo!" onclick="LimitAttach(this.
fixed problems:
1.
submit cannot be the name of an element on your form if you want to use the submit() method of the form.
javascript will always think you are talking about the button, and not the action of submitting the form (thanks CJS)
2.
let's use lastIndexOf, that's what it is there for :)
<HTML>
<HEAD><SCRIPT LANGUAGE="JavaScript">
<!-- Hide script from older browsers
extArray = new Array(".gif", ".jpg", ".png");
function LimitAttach(form, file) {
allowSubmit = false;
if (!file) return;
filename=file.substring(fi le.lastInd exOf('\\') +1,file.le ngth-4)
ext=(file.substr(file.leng th-4,4))
for(i=0;i<extArray.length; i++){
if (extArray[i] == ext) { allowSubmit=true}
}
if (allowSubmit) form.submit()
else
alert("Please only upload files that end in types: "
+ (extArray.join(" ")) + "\nPlease select a new "
+ "file to upload and submit again.");
}
// End -->
</SCRIPT>
<TITLE></TITLE>
</HEAD>
<BODY>
<P>Click "Browse" to choose the photo from your computer.<BR> Please upload
only images that end in: <SCRIPT> document.write(extArray.jo in(" "));
</SCRIPT><BR> Then click "Upload Your Photo" to upload
it </P>
<FORM METHOD="POST" ACTION="newphoto.asp" ENCTYPE="multipart/form-da ta"
ID="form1" NAME="form1"> <INPUT TYPE="file" NAME="UploadForm"
SIZE="30"><BR><BR>
<INPUT TYPE="button" NAME="submit1" VALUE="Upload Your Photo!"
ONCLICK="LimitAttach(this. form, this.form.UploadForm.value )"> </FORM></BODY>
</HTML>
Bob
1.
submit cannot be the name of an element on your form if you want to use the submit() method of the form.
javascript will always think you are talking about the button, and not the action of submitting the form (thanks CJS)
2.
let's use lastIndexOf, that's what it is there for :)
<HTML>
<HEAD><SCRIPT LANGUAGE="JavaScript">
<!-- Hide script from older browsers
extArray = new Array(".gif", ".jpg", ".png");
function LimitAttach(form, file) {
allowSubmit = false;
if (!file) return;
filename=file.substring(fi
ext=(file.substr(file.leng
for(i=0;i<extArray.length;
if (extArray[i] == ext) { allowSubmit=true}
}
if (allowSubmit) form.submit()
else
alert("Please only upload files that end in types: "
+ (extArray.join(" ")) + "\nPlease select a new "
+ "file to upload and submit again.");
}
// End -->
</SCRIPT>
<TITLE></TITLE>
</HEAD>
<BODY>
<P>Click "Browse" to choose the photo from your computer.<BR> Please upload
only images that end in: <SCRIPT> document.write(extArray.jo
</SCRIPT><BR> Then click "Upload Your Photo" to upload
it </P>
<FORM METHOD="POST" ACTION="newphoto.asp" ENCTYPE="multipart/form-da
ID="form1" NAME="form1"> <INPUT TYPE="file" NAME="UploadForm"
SIZE="30"><BR><BR>
<INPUT TYPE="button" NAME="submit1" VALUE="Upload Your Photo!"
ONCLICK="LimitAttach(this.
</HTML>
Bob
ASKER
Bebonham: In the first part of your comments, show me the code. How can I submit the form?
you had it right...
but you had a button "Upload Your Photo!"
that was called submit.
I have changed it's name to submit1
that was the only real problem, (aside from my other suggestions)
the code was correct, it was the naming of the elements in the form that was problematic.
Bob
but you had a button "Upload Your Photo!"
that was called submit.
I have changed it's name to submit1
that was the only real problem, (aside from my other suggestions)
the code was correct, it was the naming of the elements in the form that was problematic.
Bob
ASKER
5 to 50
ASKER
I will test it tonight.
thanks for the update,
good luck!
good luck!
ASKER
I did change from button to submit. I uploaded photo that ends in GIF and it took me to the asp page. But, if I uploaded photo that ends in TXT and I got the message box and it took me to the asp page. If I got the message box, it should not go anywhere, select another image with GIF or JPG or PNG. Any ideas?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
if you use the one with the submit button then this line should be
if (allowSubmit) return true
else{
and not
if (allowSubmit) form.submit()
else{
as I posted it previously.
it will work either way, but technically, it is not right.
Bob
if (allowSubmit) return true
else{
and not
if (allowSubmit) form.submit()
else{
as I posted it previously.
it will work either way, but technically, it is not right.
Bob
ASKER
Bob: I modified your code and it worked fine. Thanks a lot!
<SCRIPT language="JavaScript">
<!-- Hide script from older browsers
extArray = new Array(".gif", ".jpg");
function filetype(form, file) {
allowSubmit = false;
if (!file) return;
while (file.indexOf("\\") != -1)
file = file.slice(file.indexOf("\ \") + 1);
ext = file.slice(file.indexOf(". ")).toLowe rCase();
for (var i = 0; i < extArray.length; i++) {
if (extArray[i] == ext) { allowSubmit = true; }
}
if (allowSubmit) form.submit();
else {
alert("Please only upload files that end in types: "
+ (extArray.join(" ")) + "\nPlease select a new "
+ "file to upload and submit again.");
return false;
}}
// End -->
</script>
<p><font face="Arial" size="2" color="#000000"><center>
Please upload only images that end in:
<script>
document.write(extArray.jo in(" "));
</script><BR>
Click "Browse" to choose the photo from your computer.<BR>
Then click "Upload Your Photo" to upload it</center></font>
<BR>
<form method="POST" action="newphoto.asp" enctype="multipart/form-da ta" id=form1 name=form1>
<input type="file" name="UploadForm" size="30"><BR><BR>
<input type="submit" name="submit" value="Upload Your Photo!" onclick=" return filetype(this.form, this.form.UploadForm.value )"></CENTE R>
</form>
<SCRIPT language="JavaScript">
<!-- Hide script from older browsers
extArray = new Array(".gif", ".jpg");
function filetype(form, file) {
allowSubmit = false;
if (!file) return;
while (file.indexOf("\\") != -1)
file = file.slice(file.indexOf("\
ext = file.slice(file.indexOf(".
for (var i = 0; i < extArray.length; i++) {
if (extArray[i] == ext) { allowSubmit = true; }
}
if (allowSubmit) form.submit();
else {
alert("Please only upload files that end in types: "
+ (extArray.join(" ")) + "\nPlease select a new "
+ "file to upload and submit again.");
return false;
}}
// End -->
</script>
<p><font face="Arial" size="2" color="#000000"><center>
Please upload only images that end in:
<script>
document.write(extArray.jo
</script><BR>
Click "Browse" to choose the photo from your computer.<BR>
Then click "Upload Your Photo" to upload it</center></font>
<BR>
<form method="POST" action="newphoto.asp" enctype="multipart/form-da
<input type="file" name="UploadForm" size="30"><BR><BR>
<input type="submit" name="submit" value="Upload Your Photo!" onclick=" return filetype(this.form, this.form.UploadForm.value
</form>
glad to see you have it working!!
ASKER