Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Submitting a form

Status
Not open for further replies.

jones54

Programmer
Sep 2, 2003
23
IE
Hi,

I have a Javascript function which contains the following condition:

else {
document.forms["prices"].action = '/public/GenericPublicPage?page=careers';

document.forms["prices"].submit();
return true;

}

Based on some testing that I have been doing it seems like the each of the lines
document.forms["prices"].submit();
and
return true;

is causing the action to be submitted (therefore submitting the form twice). I wasn't expecting the "return true;" line to submit the form.

Does anyone know if "return true" automatically causes a form action to be submitted.

Many thanks for any help / suggestions.
 
If you are calling the function from an onsubmit handler that is used to stop or allow the submission, then yes - returning true WILL cause the form to submit.

If this is the case, then you do not need to explicitly call the sumbut method.

If this doesn't help, perhaps you can show some relevant code (like the whole function, how it's being called, etc).

Hope this helps,
Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thanks Dan,

Still a little confused ...

The form is as follows:
<form id="prices" method="POST" action="" >
onclick="return submit_PricesCharts();
</form>

The Javascript function is:

function submit_PricesCharts() {
document.forms["prices"].action = '/public/GenericPublicPage?page=careers';

document.forms["prices"].submit();
return true;
}
}

When I run this function it submits the action twice. I understand why the line "document.forms["prices"].submit();" submits the form but I'm not sure why the "return true" causes the form to be submitted again because the action in the HTML form is blank.

It sems to be causing the form action setup in the javascript to be submitted. Wasn't expecting this.


Thanks for your help
 
The form is as follows:
<form id="prices" method="POST" action="" >
onclick="return submit_PricesCharts();
</form>

Then why bother having a form, as you've no form elements (or, if you do, you're not showing them to us)?

Can you at least show us the form element that has the onclick? We have no idea if it's a button type, or a submit button type. If the latter, this could be the cause.

Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Sorry, just trying to keep it as simple as possible..

The complete form is:

<form id="prices" method="POST" action="" >

<input
name="ticker" class="prices-charts"
onfocus="if(this.value==' enter company name:')this.value='';setfocus(2);"
onblur="if(this.value=='')this.value=' enter company name:';setfocus(0);"
value=" enter company name:" size="9"
alt=" enter company name:" />

<input type="image" name="pcSubmit" src="/images/search-icon.gif" width="23" height="34" onclick="return submit_PricesCharts();" />

</form>
 
Right - this is your problem. The input type image will automatically submit the form. If you do not want it to, you need to cancel the "onsubmit" event of the form itself, not the onclick event of the element used to fire the submission.

Hope this helps,
Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thanks for your help Dan.

Been a while since I have used JavaScript.

What do you mean by 'cancel "onsubmit" event of the form itself'??
 
This:

Code:
<form onsubmit="return(yourValidationFuncHere());">

will call a function called "yourValidationFuncHere" when the form is submitted via a submit button, image type, etc.

If the validation function returns the boolean value true (or, if the user has JS disabled), the submission will continue. If it returns false, then the submission will be halted.

So, your input type image need not have an onclick, as it will automatically call the form's submit method. You need to move your validation from the image.

Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thanks very much for your help Dan.

One thing that is still puzzling me. I have the javascript and html on 2 web servers (production and test environment). It works fine in the production environment - page/action is only called once. However, on the test environment the page/action is called twice for the above reasons. Everything (.js files, JAR files etc.) is identical on both machines.

Do you have any idea why the Javascript might behave differently on the 2 servers? My understanding is that it is run on the client machine. I am using the same client to access the page on both environments.

Do you have any ideas or suggestions about what I should check.

Many Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top