Forum OpenACS Q&A: ad_form skips on_submit and after_submit code

Hi everyone,
I have a problem with the following tcl code, which I am working on for a school project: it builds a form which should pass the entries to another script, but when the user submits the values it just reloads the page, in spite of the ad_returnredirect instruction (which links to an existing file). On a further inspection, I noticed that no lines from the on_submit and after_submit blocks are executed.
What I really can't understand is that everything works fine if I take out the parameter declaration from ad_page_contract and replace it with a fixed value for istruttori_id...

ad_page_contract {
Inserimento Orario Settimanale
} {
istruttori_id:integer
}

set caption [db_string query "select a.nome || ' ' || a.cognome from scg_istruttori a where a.istruttori_id = :istruttori_id"]
set prev_page "Orario di $caption"
set page_title "Pianifica l'orario settimanale"

set context [list [list index {Scuola Guida}] [list istruttori-list {Lista Istruttori}] [list orari-list?istruttori_id=$istruttori_id $prev_page] $page_title]

ad_form -name orari \
-has_edit 1 \
-form {

{max_dow:integer(select)
{options {{"Lunedì - Sabato" "6"} {"Lunedì - Venerdì" "5"} } }
{label "Giorni"}
{html {size 1}}
}

{ora1:text
{label "Ora inizio mattino"}
{html {size 7} }
}

{ora2:text
{label "Ora fine mattino"}
{html {size 7} }
}

{ora3:text
{label "Ora inizio pomeriggio"}
{html {size 7} }
}

{ora4:text
{label "Ora fine pomeriggio"}
{html {size 7} }
}

} -on_submit {

#instructions

} -after_submit {

ad_returnredirect "orari-inse-q?max_dow=$max_dow&ora1=$ora1&ora2=$ora2&ora3=$ora3&ora4=$ora4"
ad_script_abort

}

Collapse
Posted by Claudio Pasolini on
The canonical way of using ad_form is to specify a form element as key.

If you add:

istruttori_id:key

to your form declaration and also a -edit_request section (even if empty) things will work.

As a side note using ad_returnredirect in this way seems unnecessarily complicated: simply add:

-action orari-inse-q \

to the form declaration and you are done.

Collapse
Posted by Davide Mazzoni on
Thank you very much, it works now ;)
Collapse
Posted by Dave Bauer on
I am not sure why you are using a form to collect the data and then redirecting with all the data to another script.

You should put all the processing instructions in the on_submit block.

Also if you need to redirect look at using

export_vars

procedure to build the URL instead of creating a string.

Collapse
Posted by David Siktberg on
Re Dave's comment on proper usage, I would love to learn how to use ad_form, but the documentation I have found seems light on examples, the ad_form code seemed a bit dense to read and understand on its own, and therefore a long sit-down and read-thru and pondering seemed called for, and I have never found the time to do that. Could someone be generous enough to post a good example or two showing some significant functionality in a page that obtains and processes input? That will be a fast way to learn, and I would be very, very grateful. Or if there is documentation with examples somewhere that would also be great to learn about. Thanks!!
Collapse
Posted by Ryan Gallimore on
Hi David,

There are loads of examples in the code itself - just grep ad_form.

Here is also a good reference:

https://openacs.org/storage/view/ad-form-quick-ref.pdf

And the OpenACS Tutorial:

https://openacs.org/storage/index?folder_id=2855304

Cheers.

Collapse
Posted by Gustaf Neumann on
See for a collection of articles as well under: https://openacs.org/rubick/