Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CGI & Forms & Validation 2

Status
Not open for further replies.

KizMar

Technical User
Mar 13, 2002
52
US
I'm used to using JavaScript to validate data input when the "Submit" button is clicked, if validation checks out, I change a hidden field to "ON", and submit again to catch the VBScript / PHP function that actually builds and sends the e-mail with the form input in HTML format (so it looks nice & neat).

I'm now on a server where I have to use CGI to build and send the e-mail. Stuff like &quot;<input type=hidden name=&quot;recipient&quot; value=&quot;email@your.host.com&quot;>&quot;.

I can't for the life of me think of how I can encapsulate the CGI tags into some sort of function I can call when the validation is checked out.

As you may be able to tell I'm kinda new at this for validation stuff and only knew how to do it one way. This could be as easy as pie but I've never had to do it this way.

Please help!
 
There are many simple formmail scripts that will do field checking for you. Basically you write your CGI code to check the field and if it fails the check you return the user to the same page with the error noted.

I think the conceptual issue you -may- be having is that Javascript and VBScript run outside the request/response cycle, that is, they run in the browser itself.

CGI runs on the server, you must submit the form, process it and return errors.

THe question is, why can't you use your client side javascript to handle this error checking still? That should not be constrained by your ISP.
 
Well, here's the Code I'm trying to use:


<script language=&quot;JavaScript&quot;>
// Setting focus on the first field in the form.
function setFocus() {
document.frmForm.FirstN.focus();
}

// Check that all required fields have been filled out
function Validate() {
// Make sure required fields are not blank.
if (document.frmForm.FirstN.value == '') {
alert('Please enter your First Name.');
document.frmForm.FirstN.focus();
return;
}
if (document.frmForm.LastN.value == '') {
alert('Please enter your Last Name.');
document.frmForm.LastN.focus();
return;
}
if (document.frmForm.UserN.value == '') {
alert('Please choose an Alias.');
document.frmForm.UserN.focus();
return;
}
if (document.frmForm.Address.value == '') {
alert('Please enter your Street Address.');
document.frmForm.Address.focus();
return;
}
// Make sure address at least has a space in it.
if (document.frmForm.Address.value.split(&quot; &quot;).length < 2) {
alert('Enter a valid Street Address.');
document.frmForm.Address.focus();
return;
}
if (document.frmForm.City.value == '') {
alert('City field is not filled in.');
document.frmForm.City.focus();
return;
}
if (document.frmForm.State.value == '') {
alert('State field is not filled in.');
document.frmForm.State.focus();
return;
}
if (document.frmForm.Zip.value == '') {
alert('Zipcode field is not filled in.');
document.frmForm.Zip.focus();
return;
}
// Make sure their's at least 5 chars.
if (document.frmForm.Zip.value.length < 5) {
alert('Please enter a valid Zipcode.');
document.frmForm.Zip.focus();
return;
}
// Make sure there are the correct # of chars.
if (document.frmForm.Phone.value.length !== 12) {
alert('Enter a valid Phone Number.');
document.frmForm.Phone.focus();
return;
}
// Make sure the e-mail address is valid.
if (document.frmForm.Email.value.indexOf(&quot;@&quot;) == -1) {
alert('Please enter a valid E-mail address.');
document.frmForm.Email.focus();
return;
}
// Verify that the password has enough chars.
if (document.frmForm.Password.value.length < 7) {
alert('Password must be at least seven (7) characters long.');
document.frmForm.Password.focus();
return;
}
// Make sure password matches the pass verification. If they don't, clear them.
if (document.frmForm.Password.value !== document.frmForm.PassVerify.value) {
alert('Password verification does not match.');
document.frmForm.Password.value = '';
document.frmForm.PassVerify.value = '';
document.frmForm.Password.focus();
return;
}
// Make sure the domain has at least a dot in it.
if (document.frmForm.Domain.value.indexOf(&quot;.&quot;) == -1) {
alert('Please enter a valid Domain Name.');
document.frmForm.Domain.focus();
return;
}
// If everything is filled in, kick out the jams
document.frmForm.Switch.value='ON';
document.frmForm.submit()
}
</script>



<html>
<head>
<title>form50</title>

</head>

<body bgcolor=&quot;#FFFFFF&quot; leftmargin=&quot;0&quot; topmargin=&quot;0&quot; marginwidth=&quot;0&quot; marginheight=&quot;0&quot;>

<table width=&quot;100%&quot; align=&quot;left&quot; cellpadding=&quot;1&quot; cellspacing=&quot;1&quot;>
<tr>
<td height=&quot;25&quot; bgcolor=&quot;FFFFCC&quot;></td>
</tr>
<tr>
<td><form name=&quot;frmForm&quot; id=&quot;frmForm&quot; method=&quot;POST&quot; action=&quot;Validate()&quot;>
<table width=&quot;400&quot; align=&quot;center&quot; cellpadding=&quot;2&quot; cellspacing=&quot;2&quot; bgcolor=&quot;#CCCCCC&quot;>
<tr>
<td colspan=&quot;5&quot; class=&quot;RedTitle&quot;><div align=&quot;center&quot;>Profile Setup</div></td>
</tr>
<tr>
<td colspan=&quot;5&quot; class=&quot;SubTitle&quot;>Personal Information</td>
</tr>
<tr>
<td width=&quot;103&quot;></td>
<td colspan=&quot;2&quot; class=&quot;FieldHints&quot;>* First</td>
<td width=&quot;30&quot; class=&quot;FieldHints&quot;>Middle</td>
<td width=&quot;170&quot; class=&quot;FieldHints&quot;>* Last</td>
</tr>
<tr>
<td class=&quot;FieldTitle&quot;>Name:</td>
<td colspan=&quot;2&quot;><input name=&quot;FirstN&quot; id=&quot;FirstN&quot; type=&quot;text&quot; size=&quot;12&quot; maxlength=&quot;30&quot; class=&quot;TextBox&quot;>
</td>
<td><input name=&quot;MiddleN&quot; id=&quot;MiddleN3&quot; type=&quot;text&quot; size=&quot;5&quot; maxlength=&quot;15&quot; class=&quot;TextBox&quot;></td>
<td><input name=&quot;LastN&quot; id=&quot;LastN3&quot; type=&quot;text&quot; size=&quot;12&quot; maxlength=&quot;30&quot; class=&quot;TextBox&quot;></td>
</tr>
<tr>
<td class=&quot;FieldTitle&quot;>* Alias:</td>
<td colspan=&quot;4&quot; class=&quot;FieldHints&quot;><input name=&quot;UserN&quot; id=&quot;UserN&quot; type=&quot;text&quot; size=&quot;20&quot; maxlength=&quot;20&quot; class=&quot;TextBox&quot;>
User Name</td>
</tr>
<tr>
<td class=&quot;FieldTitle&quot;>* Address:</td>
<td colspan=&quot;4&quot;> <input name=&quot;Address&quot; id=&quot;Address&quot; type=&quot;text&quot; size=&quot;40&quot; maxlength=&quot;100&quot; class=&quot;TextBox&quot;></td>
</tr>
<tr>
<td class=&quot;FieldTitle&quot;>* City:</td>
<td colspan=&quot;4&quot;><input type=&quot;text&quot; name=&quot;City&quot; id=&quot;City&quot; class=&quot;TextBox&quot;></td>
</tr>
<tr>
<td class=&quot;FieldTitle&quot;>* State:</td>
<td width=&quot;30&quot;> <input name=&quot;State&quot; id=&quot;State&quot; type=&quot;text&quot; size=&quot;2&quot; maxlength=&quot;2&quot; class=&quot;TextBox&quot;>
</td>
<td width=&quot;35&quot; class=&quot;FieldTitle&quot;>* Zip:</td>
<td colspan=&quot;2&quot;><input name=&quot;Zip&quot; id=&quot;Zip&quot; type=&quot;text&quot; size=&quot;10&quot; maxlength=&quot;10&quot; class=&quot;TextBox&quot;></td>
</tr>
<tr>
<td colspan=&quot;5&quot; height=&quot;15&quot;></td>
</tr>
<tr>
<td colspan=&quot;5&quot; class=&quot;SubTitle&quot;>Contact Information</td>
</tr>
<tr>
<td class=&quot;FieldTitle&quot;>* Phone:</td>
<td colspan=&quot;4&quot; class=&quot;FieldHints&quot;><input name=&quot;Phone&quot; type=&quot;text&quot; id=&quot;Phone&quot; value=&quot;&quot; size=&quot;14&quot; maxlength=&quot;14&quot; class=&quot;TextBox&quot;>
###-###-####</td>
</tr>
<tr>
<td class=&quot;FieldTitle&quot;>Mobile:</td>
<td colspan=&quot;4&quot; class=&quot;FieldHints&quot;><input type=&quot;text&quot; name=&quot;Mobile&quot; id=&quot;Mobile&quot; size=&quot;14&quot; maxlength=&quot;14&quot; class=&quot;TextBox&quot;>
###-###-####</td>
</tr>
<tr>
<td class=&quot;FieldTitle&quot;>* E-mail:</td>
<td colspan=&quot;4&quot;><input name=&quot;Email&quot; type=&quot;text&quot; id=&quot;Email&quot; size=&quot;40&quot; maxlength=&quot;100&quot; class=&quot;TextBox&quot;></td>
</tr>
<tr>
<td colspan=&quot;5&quot; height=&quot;15&quot;></td>
</tr>
<tr>
<td colspan=&quot;5&quot; class=&quot;SubTitle&quot;>Security Information</td>
</tr>
<tr>

</tr>
<tr>
<td colspan=&quot;5&quot; height=&quot;15&quot;></td>
</tr>
<tr>
<td colspan=&quot;5&quot; class=&quot;SubTitle&quot;></td>
</tr>
<tr>
<td class=&quot;FieldTitle&quot;></td>
<td colspan=&quot;4&quot;></td>
</tr>
<tr>
<td class=&quot;FieldTitle&quot;>* Domain Name:</td>
<td colspan=&quot;4&quot;><input name=&quot;Domain&quot; type=&quot;text&quot; class=&quot;TextBox&quot; id=&quot;Domain&quot; value=&quot;&quot; size=&quot;40&quot; maxlength=&quot;100&quot;></td>
</tr>
<td></td>
<td colspan=&quot;4&quot; class=&quot;FieldHints&quot;>Example: your_domain.com</td>
<tr>
<td colspan=&quot;5&quot; height=&quot;15&quot;></td>
</tr>
<tr>
<td colspan=&quot;4&quot;>
<div align=&quot;center&quot;>
<input type=&quot;hidden&quot; id=&quot;Switch&quot; name=&quot;Switch&quot; value=&quot;OFF&quot;>
<input type=&quot;button&quot; name=&quot;btnSubmit&quot; id=&quot;btnSubmit&quot; value=&quot;Step Two&quot; class=&quot;RedBtn&quot; onClick=&quot;Validate()&quot; accesskey=&quot;s&quot;>

</div></td>
<td><div align=&quot;center&quot;><input type=&quot;reset&quot; name=&quot;btnReset&quot; id=&quot;btnReset&quot; value=&quot;Reset&quot; class=&quot;RedBtn&quot;></div></td>
</tr>
<tr bgcolor=&quot;#999999&quot;>
<td colspan=&quot;5&quot;><div align=&quot;center&quot;><font color=&quot;#FFFFFF&quot; size=&quot;2&quot;><strong>* Indicates required fields.</strong></font></div></td>
</tr>
</table>

*******------------------------------
*******This part here is where I'm confused:
*******------------------------------
// ??? How do I use PHP and/or JavaScript to run this part of the CGI when the &quot;Switch&quot; vairable is &quot;ON&quot;... Also, if I'm using the submit button to run my JavaScript (form action is the Javascript function), how do i also tell the CGI that the form action is &quot;/cgi-bin/FormMail.cgi&quot; after that??? I think I'm missing some really easy way to do this.

<?php if (isset($_POST['Switch']) && ('ON' == $_POST['Switch'])) { ?>
<input method=&quot;POST&quot; action=&quot;/cgi-bin/FormMail.cgi&quot;>
<input type=hidden name=&quot;recipient&quot; value=&quot;myEmail@domain.net&quot;>
<input type=hidden name=&quot;email&quot; value=&quot;<?php $_POST['Email'] ?>&quot;>
<input type=hidden name=&quot;subject&quot; value=&quot;Test&quot;>
<? } ?>

</form>
</td>
</tr>
</table>

</body>
</html>
 
siberian is right - you do your form validation as you always have done - even if the form is CGI generated. A web page resides on the users machine, not the server, so continue to use javascript as you always have.

There's always a better way. The fun is trying to find it!
 
I'm still not sure how to do it though. I don't understand how I can execute the following code:
&quot;
<input type=hidden name=&quot;recipient&quot; value=&quot;myEmail@domain.net&quot;>
<input type=hidden name=&quot;email&quot; value=&quot;<?php $_POST['Email'] ?>&quot;>
<input type=hidden name=&quot;subject&quot; value=&quot;Test&quot;>
&quot;
When the &quot;<form...>&quot; &quot;method=&quot;POST&quot;&quot; and &quot;action=&quot;/cgi-bin/FormMail.cgi&quot; need to be used, and I'm already using the &quot;action&quot; to call the &quot;Validate()&quot; function.

When they hit submit, right now the &quot;action&quot; points to the JavaScript validation function &quot;Validate()&quot;. When the validations checks to be &quot;OK&quot;, I turn a hidden value to &quot;ON&quot;. The next step after that is to call a function with the code to send the e-mail. That's the part I'm not seeing. If I'm doing that part with JavaScript also, how do I use the &quot;<form action=&quot;/cgi-bin/FormMail.cgi&quot; method when that form already has an action that sends it to the JavaScript validation?

I'm sorry if I'm not making any sense, my eyes have been crossed for some time now on this one. =)
 
Change your form tag to read as follows:

<form name=&quot;frmForm&quot; id=&quot;frmForm&quot; method=&quot;POST&quot; action=&quot;/cgi-bin/FormMail.cgi&quot; onSubmit=return &quot;Validate(this)&quot;>

Also, in your validate function, remove this: // If everything is filled in, kick out the jams
document.frmForm.Switch.value='ON';
document.frmForm.submit()
at the end of the function - it's no longer needed. Additionally, change the rest of the &quot;if&quot; statements to read return false;.

Now, when the user clicks the Submit button, the Validate(this) function is called. All of the if statements must be true in order for the FormMail.cgi script to be called.

There's always a better way. The fun is trying to find it!
 
Now that is EXACTLY what I was looking for!!!!!!! You just saved me a LOT of time. Thank you VERY much tviman. =)

I'm able to have fun again! ;')
 
Glad to have helped...

There's always a better way. The fun is trying to find it!
 
I had this page working at one point, but now it's skipping the validation and I can't figure out why. I've tried everything from stripping it down to one field and a submit button using exactly what you (TVIMAN) told me and it still isn't working. Can you help?

Here's the URL to the page:

 
<script language=&quot;JavaScript&quot; type=&quot;text/javascript&quot; src=&quot;include/no_clicky.js&quot;></script>

This ain't helping

--Pauk
 
KizMar... you're going to kick yourself -

Put quotation marks around your onSubmit statement:

<form name=&quot;frmSetup50&quot; id=&quot;frmSetup50&quot; method=&quot;POST&quot; action=&quot;/cgi-sys/FormMail.cgi&quot; onSubmit=&quot;return Validate(this)&quot;>

Also - are your SURE about the cgi-sys? Usually it's cgi-bin.

A helping word - when you have trouble like this, put an &quot;alert&quot; statement immediately before the first &quot;if&quot; statement. This will tell you whether if the function is being entered.



There's always a better way. The fun is trying to find it!
 
Bah! I figured it would be something like that!!!

*kick's himself*

Again TVIMAN, thanks for your keen eye! ;')

The cgi-sys thing... that's the way our server host has it set up. Not sure why, but it works. =) My form is now operational... and I still have hair left.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top