f



Help: I can't figure out why this won't work

I have a hyperlink on an ASP page that calls another ASP page with the
code shown below.  This page allows (forces) a user to select a file to
be uploaded, then should call a third ASP page that will do the actual
upload.  What happens is I can enter a file name but the third page
doesn't get loaded.  I see the upload page being called in the browser
when the alert message displays, but then it appears to reload this
page.  What am I missing???  I should also note that the upload page
opens fine if I move the location.href command outside of the function
(for testing purposes).

<FORM NAME="GetFileName">

<script language=javascript>
	function callImport(fieldName) {
		// Read the value from the named field passed to this function
		fileName = document.forms[0].elements[fieldName].value;
		// Replace backslashes with forward slashes
		fileName = fileName.replace("\\","/");

		if (fileName.length > 0) {
			location.href = "upload.asp";
alert("after page redirect");
			}
		else {
			alert("You must provide a file name and path.");
		}
	}
</script>

	<br>
	<font face="Arial"><b>Select the file to load:</b></font>
	<br><br>
	<input type="file" name="fileName" size="60">
	<br><br>
	<input type=SUBMIT name="submitButton" value="Load File"
onClick="callImport('fileName')">

</form>

0
memiller
9/26/2006 2:17:14 PM
comp.lang.javascript 38370 articles. 2 followers. javascript4 (1315) is leader. Post Follow

3 Replies
916 Views

Similar Articles

[PageSpeed] 29

memiller@westernsurety.com said:
>
>I have a hyperlink on an ASP page that calls another ASP page with the
>code shown below.  This page allows (forces) a user to select a file to
>be uploaded, then should call a third ASP page that will do the actual
>upload.  What happens is I can enter a file name but the third page
>doesn't get loaded.  I see the upload page being called in the browser
>when the alert message displays, but then it appears to reload this
>page.  What am I missing???  I should also note that the upload page
>opens fine if I move the location.href command outside of the function
>(for testing purposes).
>
><FORM NAME="GetFileName">
>
><script language=javascript>
>	function callImport(fieldName) {
>		// Read the value from the named field passed to this function
>		fileName = document.forms[0].elements[fieldName].value;
>		// Replace backslashes with forward slashes
>		fileName = fileName.replace("\\","/");
>
>		if (fileName.length > 0) {
>			location.href = "upload.asp";
>alert("after page redirect");
>			}
>		else {
>			alert("You must provide a file name and path.");
>		}
>	}
></script>
>
>	<br>
>	<font face="Arial"><b>Select the file to load:</b></font>
>	<br><br>
>	<input type="file" name="fileName" size="60">
>	<br><br>
>	<input type=SUBMIT name="submitButton" value="Load File"
>onClick="callImport('fileName')">
>
></form>

The function of an input of type SUBMIT is to submit the form.
Since you haven't provided any other action for the form, that
means that the current page is reloaded from the server, which
is what you're seeing.

If you don't really want to submit the form, use an input of
type BUTTON, instead.  You should almost never use the onclick
handler of a submit button.


-- 
                         
0
Lee
9/26/2006 4:00:03 PM
Lee wrote:
> memiller@westernsurety.com said:
> >
> >I have a hyperlink on an ASP page that calls another ASP page with the
> >code shown below.  This page allows (forces) a user to select a file to
> >be uploaded, then should call a third ASP page that will do the actual
> >upload.  What happens is I can enter a file name but the third page
> >doesn't get loaded.  I see the upload page being called in the browser
> >when the alert message displays, but then it appears to reload this
> >page.  What am I missing???  I should also note that the upload page
> >opens fine if I move the location.href command outside of the function
> >(for testing purposes).
> >
> ><FORM NAME="GetFileName">
> >
> ><script language=javascript>
> >	function callImport(fieldName) {
> >		// Read the value from the named field passed to this function
> >		fileName = document.forms[0].elements[fieldName].value;
> >		// Replace backslashes with forward slashes
> >		fileName = fileName.replace("\\","/");
> >
> >		if (fileName.length > 0) {
> >			location.href = "upload.asp";
> >alert("after page redirect");
> >			}
> >		else {
> >			alert("You must provide a file name and path.");
> >		}
> >	}
> ></script>
> >
> >	<br>
> >	<font face="Arial"><b>Select the file to load:</b></font>
> >	<br><br>
> >	<input type="file" name="fileName" size="60">
> >	<br><br>
> >	<input type=SUBMIT name="submitButton" value="Load File"
> >onClick="callImport('fileName')">
> >
> ></form>
>
> The function of an input of type SUBMIT is to submit the form.
> Since you haven't provided any other action for the form, that
> means that the current page is reloaded from the server, which
> is what you're seeing.
>
> If you don't really want to submit the form, use an input of
> type BUTTON, instead.  You should almost never use the onclick
> handler of a submit button.

Typically it is better to set the action attribute of the form tag and
use the onsubmit event of the form tag to call your javascript
function. Ideally this function will return true (if you want the form
to submit) or false (if you don't).

For example:

<script type="text/javascript">
function checkForm() {
  var doSubmit = false;
  // check your form elements and set doSubmit to true or false...
  return doSubmit;
}
<script>
.....
<form action="page2.asp" onsubmit="return checkForm();">
....
</form>

Hope that makes sense.

> 
> 
> --

0
Tom
9/26/2006 4:40:31 PM
Lee wrote:
> The function of an input of type SUBMIT is to submit the form.
> Since you haven't provided any other action for the form, that
> means that the current page is reloaded from the server, which
> is what you're seeing.
>
> If you don't really want to submit the form, use an input of
> type BUTTON, instead.  You should almost never use the onclick
> handler of a submit button.

Lee, Tom:

Thanks for your help and suggestions.  Changing to type BUTTON solved
my problem.  I will work on the other suggestions you pointed out on
the action.  Thanks again!!!

0
memiller
9/26/2006 4:59:02 PM
Reply: