Webprogr: Link with automatic submit

G

Gregor Horvath

Hi,

As I am no expert web programmer I thought I'd better ask the experts if
there is a simpler or better solution to my problem than the one I am
thinking of. (using TurboGears)

The problem
-----------

I have a form. Ok. you can submit, validate it etc.
Now I have a link on that form to another page.
What I want is that the form gets automatically submitted and validated
before the browser follows the link. If the validation has errors the
link should not be followed.

My solution
-----------

1. client side solution

First I thought I just make a little javascript function like this:

function xy(url) {
document.form.submit();
parent.location=url;
}

The problem is that submit works ansynchronisly. I do not get a return
code to decide if to follow the url or not. Furthermore the form does
not get submitted if there is the line parent.location=url; .

2. server side solution

I thought of including a hidden field and include directives for my
controller method to raise the redirection if no validation errors are
there.

<a href="#" onClick="xy('/somewhere')"></a>

function xy(url) {
document.form.hiddenfield.value = "redirect_to: " + url;
document.form.submit();
}

@expose
def mycontroller(self, *args, **kwargs):
#do the usual saving stuff

if hasattr(kwargs, 'hiddenfield'):
#parse the JSON or mini-langage and do the redirection, when no
#validation errors


As there might be more other directives necessery for client-server
communciation I thought of setting the hiddenfield value to JSON or a
self made mini-language and parse this in the controller.

Is this a good solution?
Are there any other options?
 
B

Bruno Desthuilliers

Gregor said:
Hi,

As I am no expert web programmer I thought I'd better ask the experts if
there is a simpler or better solution to my problem than the one I am
thinking of. (using TurboGears)

The problem
-----------

I have a form. Ok. you can submit, validate it etc.
Now I have a link on that form to another page.
What I want is that the form gets automatically submitted and validated
before the browser follows the link. If the validation has errors the
link should not be followed.

My solution
----------- (snip)
Are there any other options?

<OT>
yes : replace the link with another submit button, then in your
controller check which submit has been successful and take appropriate
action.

Links are for GET request (which are supposed to be idempotent), and are
definitively *not* supposed to issue (even if indirectly) a POST request.
</OT>
 
G

Gregor Horvath

Bruno said:
yes : replace the link with another submit button, then in your
controller check which submit has been successful and take appropriate
action.

Thanks !
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top