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

M

memiller

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>
 
L

Lee

(e-mail address removed) 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.


--
 
T

Tom Cole

Lee said:
(e-mail address removed) 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.
 
M

memiller

Lee said:
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!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top