Link to home
Start Free TrialLog in
Avatar of computer1000
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-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)">

Avatar of computer1000
computer1000

ASKER

I will go to the Community Support to ask for deleting a few questions that I have solved.
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
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
5 to 50
I will test it tonight.
thanks for the update,

good luck!
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
Avatar of bebonham
bebonham

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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>
glad to see you have it working!!