PDA

View Full Version : how to submit javascript validated form to CGI script


Carlitos
11-19-2002, 09:42 PM
Apologies for the basic level of this question. I am using a javascript that forces the user to rank order responses on a survey form.

However I am not clear on syntax. The java script itself is at the bottom of this message. Within it, I assume that I need to change:

document.clear();
document.write("success! it be sweet
\n");

to submit();or document.form.submit(); in order allow the form to post to a cgi script, but am not sure of this syntax to use. Am I even close to being on the right track with this change?

Also, am I not sure if I would need to change the HTML form tag in any way:

<FORM ACTION="../cgi-bin/survey.cgi" METHOD="POST">

Thanks for any and all help. Here is the script:

<SCRIPT LANGUAGE="JavaScript">

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
<!-- Original: Kent Rauch (kent@restekcorp.com) -->
<!-- Web Site: http://www.restekcorp.com -->
<!-- Begin
function validator() {
// copyright 2002 Kent Rauch

// global declaration
badrank = false;

rankem(1,4);
rankem(2,5);

if (!badrank) {
// this is a "phony submit" for testing purposes
document.clear();
document.write("success! it be sweet<br>\n");
}

}

// ---------------------------------------------------------
// Validate ranking questions: each value used exactly once.

function rankem(question, q_size) {
// copyright 2002 Kent Rauch

var aLert1 = "";
var aLert2 = "";

// supports up to 26 items to be ranked -- extend this array to increase
var cal = "a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z";
cal = cal.split('.');

var a = 0;

var irate = "rink" + question;

eval(irate + " = new Object();");

var myrate = "";

for (var x = 0; x < q_size; ++x) {
myrate = "q"+question+cal[x];
eval(irate + "[" + x + "] = document.test_form." + myrate + ".selectedIndex");
if (eval(irate + "[" + x + "]")) {
++a;
for (var y = 0; y < x; ++y) {
if (eval(irate + "[" + y + "]") == eval(irate + "[" + x + "]")) {
aLert1 = "Question "+ question +": please use each ranking only once.\n";
}
}
}
}

if (a != q_size) {
aLert2 = "Question " + question +":please rank all items.\n";
}

var aLert = aLert1 + aLert2;

if (aLert) {
alert(aLert);
badrank = true;
}


}
// End -->
</script>

glenngv
11-20-2002, 01:21 AM
yup, change it to:

document.test_form.submit();

and you don't have to change the form tag

whammy
11-20-2002, 01:34 AM
The boolean return seems pretty convoluted in that script. :eek:

Carlitos
11-20-2002, 04:31 AM
Thanks! OK, I took out the

document.clear();
document.write("success! it be sweet<br>\n");

and put in

document.test_form.submit();

I left the form tag as is.

However, it now posts even when the alerts have been triggered.

Any ideas?

glenngv
11-20-2002, 05:05 AM
are you calling validator() like this:

<input type="submit" value="Rank" name="btnRank" onclick="validator()">

it should be input type=button

Carlitos
11-20-2002, 01:32 PM
Great! That helped. I now have this at the bottom of the form:

<INPUT TYPE="hidden" NAME="survey_name" VALUE="staff">
<input type="button" onClick="validator();" VALUE="SUBMIT">

The first tag is a requirement of the cgi script.

Now, the alerts work, but it does not submit even when the values are all filled out and unique.

What did I do wrong this time?

pilla
11-20-2002, 09:13 PM
Try removing ; after validator()

<INPUT TYPE="hidden" NAME="survey_name" VALUE="staff">
<input type="button" onClick="validator();" VALUE="SUBMIT">

Carlitos
11-20-2002, 09:24 PM
That did it!! Thanks!!