Form submission.

They have: 3 posts

Joined: Oct 2000

How can I script a form so that the user is required to enter information (within certain fields)before the form will allow them to submit their info.

cnick84552

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

Use JavaScript to check each field on submit, or even sooner.

http://tech.irt.org/articles/js049/index.htm is an online tutorial that explains how to do it.

I must mention here that I link to existing tutorials that I have personally used, and know work -- go read the tutorial! (This isn't personal to you, Neptune)

Suzanne

Vincent Puglia's picture

They have: 634 posts

Joined: Dec 1999

Hi Neptune,

Suzanne gave you a link to an excellent source (irt.org).

There are a number of ways to check for user non-user. The method I use is to loop through the form elements by type and take appropriate action from there. For example:

function chkForm(formObj)
{
var isOk2Send = true;
with (formObj)
{
for (i=0; i < length; i++)
if (elements[i].type == 'text')
if (elements[i].value == "")
{
isOk2Send = false;
alert("You must fill in the field.");
elements[i].focus();
}
}
if (isOk2Send) formObj.submit();
}

....blah blah....

In the above example, a normal button (instead of a submit button) is used to call the validation routine. The actual submission is determined by the isOk2Send variable. If it is still true after cycling through the fields, submission is executed (the action in the form tag is used). If it is false, nothing happens.

If you would like a longer explanation and/or how to test other types (radio buttons, selection lists, etc), see the "Validating Forms" script/tutorial at my site.

Vinny

Where the world once stood
the blades of grass cut me still

mjames's picture

They have: 2,064 posts

Joined: Dec 1999

There's a simpler way to do this.

In your form HTML, add this tag:

<INPUT type=hidden name="whatever,whatever">'

Just add the names of the fields in your form that you want to be required in the above tag. Should work.

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

But if you use any other form processor, it probably won't.

Additionally, FormMail.pl is a cgi application, so the user will have to submit the form and *then* find out that it's not done right. Using JavaScript, you can check the form as the user fills it out (onBlur()), or when the whole form is done so it doesn't go for submission until it's ready (onSubmit()).

JavaScript is better for the user (less chance of error) and for the person getting the forms (less wrong submissions), but yes, it does assume JavaScript is active.

Ideally, you would use JavaScript AND a server side solution like CGI (or ASP, et cetera), to cover your bases.

Smiling Suzanne

P.S. Edited to add: the correct code for the required field for FormMail.pl is

mjames's picture

They have: 2,064 posts

Joined: Dec 1999

You can get the formail.pl from Matt's Script Archive, I believe.

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

it won't help! the question was how to get the form checked BEFORE it was submitted. No matter what form processor you use, that pre-submission check has to be done with a CLIENT SIDE script, not a server side script.

Please! Marc, I mean you no disrespect, but your solution is for a different problem, not this one.

Suzanne

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.