Click to See Complete Forum and Search --> : [RESOLVED] Trouble with required fields in forms/email


sheepo-designs
02-13-2006, 10:20 AM
I want to set up a newsletter set up form, where the user can submit their name, email, membership number, and postal code, and if their information matches mine (dealing with databases later) then they will goto a page that says "thank you" I have that fine, and a copy also ends up in my inbox. My problem is; if a user leaves the membership number, name, or email field blank, I want them to be directed to a page saying "error" nothing about what error, just that there was an error and they should try again.

Can anyone give me a hand?

Thanks.

Kravvitz
02-13-2006, 12:04 PM
You need to use a server-side language, like PHP, CGI/PERL, or ASP, to do that.

Find out which language(s) your server supports and ask a moderator to move this thread for you.

NogDog
02-13-2006, 02:03 PM
// substitute the names of the required fields here:
$required = array('member_number', 'name', 'email');
foreach($required as $value)
{
if(!isset($_POST[$value]) or trim($_POST[$value] == "" )
{
header("Location: http://www.yoursite.com/error.html");
exit;
}
}
// continue with normal form processing...

sheepo-designs
02-13-2006, 10:50 PM
Where do I put that? In the Head?

sridhar_423
02-13-2006, 10:54 PM
thts PHP, a server-side language..
put it at the top of your page preferably as there is a header.

sridhar_423
02-13-2006, 10:56 PM
why dont you use Javascript to alert the user when he leaves the fields blank. Redirecting the user to an error page for such a simple fault frustates the user.

NogDog
02-13-2006, 10:57 PM
Yes, it needs to go at the beginning of the file (between <?php ?> tags) before anything that would output to the browser (including before any HTML code, or even blank lines or spaces not inside of <?php ?> tags).

sheepo-designs
02-13-2006, 10:58 PM
oooooooh ok. I'll give it a shot.

sheepo-designs
02-13-2006, 11:03 PM
Still not working, here's the source code, maybe it'll help

<?php
$required = array('realname', 'membernumber', 'email');
foreach($required as $value)
{
if(!isset($_POST[$value]) or trim($_POST[$value] == "" )
{
header("Location: http://www.yoursite.com/error.html");
exit;
}
}
?>
<html>
<head>
<title>.:Oshawa Power and Sail Squadron:.</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="opss_CSS.css">
</head>
<body class="background">

<table id="Table_01" width="820" height="600" border="0" cellpadding="0" cellspacing="0" align="center">
<tr>
<td colspan="3" background="images/banner.gif" width="820" height="155" class="noEXT"></td>
</tr>
<tr>
<td background="images/NAV.gif" width="212" height="403" class="noEXT" id="NAV" valign="baseline">
<div align="center">
<a class="linkTEXT" href="home.htm">Home</a><br>
<a class="linkTEXT" href="events.htm">Events &amp; Course Dates</a><br>
<a class="linkTEXT" href="newsletters.htm">Newsletters</a><br>
<a class="linkTEXT" href="officers.htm">Officers</a><br>
<a class="linkTEXT" href="history.htm">History</a><br>
<a class="linkTEXT" href="memberlogin.htm">Member's Page</a><br>
<a class="linkTEXT" href="benefits.htm">Benefits</a><br>
<a class="linkTEXT" href="classifieds.htm">Classifieds</a><br>
<a class="linkTEXT" href="links.htm">Links</a><br><br>
<a class="linkTEXT" href="contact.htm">Contacts</a><br><br>
<p class="normTEXT">Comments? Questions?</p>
<form method="post" action="http://www.oshawapss.ca/formmail.php" name="SampleForm">
<input type="hidden" name="env_report" value="REMOTE_HOST,REMOTE_ADDR,HTTP_USER_AGENT,AUTH_TYPE,REMOTE_USER" />
<input type="hidden" name="subject" value="Message From Oshawa PSS" />
<input name="bad_url" type="HIDDEN" value="error.htm" />
<input name="good_url" type="HIDDEN" value="thankyou.htm" />
<input name="requirements" type="HIDDEN" value="realname:Your Name,email:Your Email Address" />
<input name="alert_to" type="HIDDEN" value="sheepo.design@gmail.com" />
<input name="recipients" type="HIDDEN" value="sheepo.design@gmail.com,oshawa.pss@gmail.com" />

<input name="realname" type="text" value="name" size="20" maxlength="40" class="textFIELD" /><br><br>
<input name="email" type="text" value="email" size="20" maxlength="40" class="textFIELD" /><br><br>
<textarea name="comments" class="textFIELD" maxlength="1000">Comments</textarea><br><br>
<input type="submit" value="send" class="button" />
</form>
</div>
</td>
<td rowspan="2" bgcolor="#DFCCAE" width="586" height="410" class="vertEXT" valign="top">
<p class="normTEXT">If you would like to receive up-to-date news about our squadron, then please sign up for our newsletter. However, you need to be a member in order to sign up, a membership number with corresponding name and postal code will be requested.<br>
<font color="#FF0000">*</font> = Required</p>
<form method="post" action="http://www.oshawapss.ca/formmail.php" name="SampleForm">
<input type="hidden" name="env_report" value="REMOTE_HOST,REMOTE_ADDR,HTTP_USER_AGENT,AUTH_TYPE,REMOTE_USER" />
<input type="hidden" name="subject" value="Request to sign up for Newsletter" />
<input name="bad_url" type="HIDDEN" value="error.htm" />
<input name="good_url" type="HIDDEN" value="thankyou.htm" />
<input name="requirements" type="HIDDEN" value="realname:Your Name,email:Your Email Address,membernumber:Your Membership Number,postal:Your Postal Code" />
<input name="alert_to" type="HIDDEN" value="sheepo.design@gmail.com" />
<input name="recipients" type="HIDDEN" value="sheepo.design@gmail.com,oshawa.pss@gmail.com" />

<input name="realname" type="text" value="Name" size="20" maxlength="40" class="textFIELD" /><font color="#FF0000">*</font><br><br>
<input name="email" type="text" value="Email" size="20" maxlength="40" class="textFIELD" /><font color="#FF0000">*</font><br><br>
<input name="postal" type="text" value="Postal" size="20" maxlength="7" class="textFIELD" /><p class="normTEXT">(X1X 1X1)</p>
<input name="memnumber" type="text" value="Member #" size="20" maxlength="9" class="textFIELD" /><font color="#FF0000">*</font><p class="normTEXT">(123456ABC)</p>
<input type="submit" value="Send" class="button" />
</form>

<p class="titleTEXT">"Snail Mail" option</p>
<p class="normTEXT">If you dont have an e-mail address and would like to receive our newsletter by mail, please contact our <a href="mailto:aaron@creativefusionart.com" class="linkTEXT">Newsletter Editor</a>.</p>
</td>
<td rowspan="2" background="images/rightsider.gif" width="22" height="410" class="vertEXT"></td>
</tr>
<tr>
<td background="images/vertext.gif" width="212" class="vertEXT"></td>
</tr>
<tr>
<td colspan="3" background="images/footer.gif" width="820" height="35"></td>
</tr>
</table>

</body>
</html>

sheepo-designs
02-13-2006, 11:12 PM
why dont you use Javascript to alert the user when he leaves the fields blank. Redirecting the user to an error page for such a simple fault frustates the user.

How would this be done?

sridhar_423
02-13-2006, 11:24 PM
<form method="post" action="http://www.oshawapss.ca/formmail.php" name="SampleForm" onSubmit="return ChkforValues(SampleForm)">
.......
</form>

<Script Language=JavaScript>
function ChkforValues(form){
if(form.realname.value=="" || form.memnumber.value=="" || form.email.value==""){
alert("Name,Meme Num and Email are mandatory");
return false;
//If you want to redirect the user to some error page if blank values arwe enetered.. put these lines instead of the above lines
// form.action="Your_error_page_name";
//return true;
}
}
</Script>

sridhar_423
02-13-2006, 11:25 PM
put the <Script language.....
</script> inside the <HEAD> ..</HEAD>

NogDog
02-13-2006, 11:31 PM
You still need to to server-side validation to handle the 10% or so of users who do not have JavaScript available/enabled.

sheepo-designs
02-13-2006, 11:35 PM
It worked! Thank you sridhar!

NogDog
02-13-2006, 11:36 PM
The code I provided would need to go in the formmail.php file (the action called by your form tag). Also, I left out one parenthese you'll need to add:

if(!isset($_POST[$value]) or trim($_POST[$value]) == "" )

sheepo-designs
02-13-2006, 11:39 PM
Where in Formmail does this go, at the beginning before anything else? I really dont like messing around with formmail, so you'll have to help me with this one.

NogDog
02-13-2006, 11:43 PM
Where in Formmail does this go, at the beginning before anything else? I really dont like messing around with formmail, so you'll have to help me with this one.
Yes, I would put it as the very first thing in the file. (If you feel uneasy about it, just make a backup copy of the file first so you know you can revert back to it if things get out of hand. :) )

sheepo-designs
02-13-2006, 11:53 PM
That also works! One more question, though. If it were possible, could I have both Javascript and PHP for this form, but have some kind of code that would see if the user has Javascript and enables it, or if they dont and uses php?

NogDog
02-14-2006, 08:43 AM
Yes, the JavaScript can go on the form page to give you immediate validation on the client side, then the PHP code on the formmail page will give you the server-side validation you need for any users who do not have JS enabled.

sheepo-designs
02-14-2006, 11:34 AM
excellent :) Thanks a lot NogDog