Forum Moderators: open
I want to add autosubmit functionality to my form, but need to pass a valid button name as well. The following code submits the form and it's values, but how can I pass a submit button name/value to the perl script?
setTimeout("document.forms[0].submit()", 10000);
This does not work:
setTimeout("document.forms[0].submit()" value="mybutton", 10000);
Neither does this:
document.forms[0].mybutton.value = "myvalue";
setTimeout("document.forms[0].submit()", 10000);
When the page loads:
<input type="hidden" name="whichBut" value="default"> Then, when the visitor clicks any submit button, set it so the button click changes the value of the hidden element:
<input type="submit" onclick="this.form.whichBut.value='button1'" value="Button 1">
<input type="submit" onclick="this.form.whichBut.value='button2'" value="Button 2"> When the form is submitted, your hidden element would tell you either that no button was clicked ("whichBut=default") or which button was clicked ("whichBut=button1").
That sort of takes care of my problem (if I do some reprogramming of the Perl script and assume javascript to be available), but not quite:
Using your suggestion together with
setTimeout("document.forms[0].submit()", 10000);
returns whichBut=default whilst clicking on the actual button returns whichBut=value AND SubmitButtonName=value
What I'd like is to get (only) SubmitButtonName=value also when the autosubmit line submits the form.
<input type="hidden" name="SubmitButtonName" value="default"> at the top of the form and then:
<input type="submit" name="SubmitButtonName" value="Button 1"> lower in the form (as many as you like).
When the form result comes in, if NO submit button has been clicked, you only have the one "SubmitButtonName=default". If ANY submit button has been clicked, you end up with "SubmitButtonName=default&SubmitButtonName=Button%201".
For the most part, any script that parses this result and does NOT treat SubmitButtonName as an array will use the LAST value in the post string, overriding the first "default" value. This is probably what would happen if you tried it with your PERL script. (No Javascript required, except for your timeout!) :)
I was hoping for a pure javascript solution doing what I described, to save some re-writing of my original Perl code. Also I wanted the system to work even if javascript support was absent (obviously without the auto submit feature). Your hidden form element works if I modify my Perl code, but something like
setTimeout("document.forms[0].button['whichBut'].submit()", 10000);
...or...
setTimeout("document.forms[0].button['whichBut'].click()", 10000);
would be better in my case. But it is not possible?
Thank you for your assistance!
The most recent solution I put up will satisfy your most recent post.
1) It does not require Javascript to work (except for the auto-submit, as you noted)
2) It requires no changes to your PERL script.
By putting the hidden "default" "SubmitButtonName" element into your form and following that with a series of clickable "SubmitButtonName" buttons with various values, you get this:
a) No user click: JS auto-submit: SubmitButtonName=default
b) User clicks first submit button (let's call it "Button1"): No JS: SubmitButtonName=Button1
OR
b) User clicks second submit button (let's call it "Button2"): No JS: SubmitButtonName=Button2
Your PERL script will only "see" the LAST SubmitButtonName in the form output, so even when the default value is ALSO sent, the PERL script will choose the LAST value to override it.
i.e. This is sent to the PERL script:
SubmitButtonName=default&UserName=Jim&UserAge=40&SubmitButtonName=Button2 The PERL script reads and assigns values in order, like:
Read: SubmitButtonName
Value: default Read: UserName
Value: Jim Read: UserAge
Value: 40 Read: SubmitButtonName
Value: Button2 So at the end of reading and assigning values, the PERL script ends up using these values:
UserName=Jim UserAge=40 SubmitButtonName=Button2 There can only be one value for any element. When there are more than one of the same, NON-ARRAY* elements in a query, the LAST one overrides all of the others. This means it is safe to include a default element value that is then overridden by the user's click on one of the other elements of that same name.
*An array-type element is treated differently, but your script doesn't know from arrays with regard to the SubmitButtonName element.
I am curious what value there is for you to submit a form without the visitor being finished. Is this for a timed quiz or something?