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-data" 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(".")).toLowerCase();
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.join("  "));
   </script><BR>
   Then click "Upload Your Photo" to upload it


<form method="POST" action="newphoto.asp" enctype="multipart/form-data" 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)">

computer1000Asked:
Who is Participating?
 
bebonhamConnect With a Mentor Commented:
okay, the button doesn't have to be submit it just CANNOT be NAMED submit.


I will show two codes, one with a submit button and one with a normal button.

they will both not submit if the alert box pops.




<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(file.lastIndexOf('\\')+1,file.length-4)
ext=(file.substr(file.length-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.");
return false;
}}
//  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.join("  "));
 </SCRIPT><BR> Then click "Upload Your Photo" to upload
         it </P>
     <FORM METHOD="POST" ACTION="newphoto.asp" ENCTYPE="multipart/form-data"
      ID="form1" NAME="form1"> <INPUT TYPE="file" NAME="UploadForm"
         SIZE="30"><BR><BR>
         <INPUT TYPE="button" NAME="cantbesubmit" VALUE="Upload Your Photo!"
          ONCLICK="return LimitAttach(this.form, this.form.UploadForm.value)"> </FORM></BODY>
</HTML>




this below example is with a submit button instead




<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(file.lastIndexOf('\\')+1,file.length-4)
ext=(file.substr(file.length-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.");
return false
}}
//  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.join("  "));
 </SCRIPT><BR> Then click "Upload Your Photo" to upload
         it </P>
     <FORM ONSUBMIT="return LimitAttach(this, this.UploadForm.value)" METHOD="POST" ACTION="newphoto.asp" ENCTYPE="multipart/form-data"
      ID="form1" NAME="form1"> <INPUT TYPE="file" NAME="UploadForm"
         SIZE="30"><BR><BR>
         <INPUT TYPE="submit" NAME="submit1" VALUE="Upload Your Photo!"> </FORM></BODY>
</HTML>


hope that helps,

Bob
0
 
computer1000Author Commented:
I will go to the Community Support to ask for deleting a few questions that I have solved.
0
 
bebonhamCommented:
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(file.lastIndexOf('\\')+1,file.length-4)
ext=(file.substr(file.length-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.join("  "));
  </SCRIPT><BR> Then click "Upload Your Photo" to upload
          it </P>
      <FORM METHOD="POST" ACTION="newphoto.asp" ENCTYPE="multipart/form-data"
       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
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
computer1000Author Commented:
Bebonham: In the first part of your comments, show me the code. How can I submit the form?
0
 
bebonhamCommented:
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
0
 
computer1000Author Commented:
5 to 50
0
 
computer1000Author Commented:
I will test it tonight.
0
 
bebonhamCommented:
thanks for the update,

good luck!
0
 
computer1000Author Commented:
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?
0
 
bebonhamCommented:
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
0
 
computer1000Author Commented:
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(".")).toLowerCase();
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.join("  "));
      </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-data" 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)"></CENTER>
  </form>
0
 
bebonhamCommented:
glad to see you have it working!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.