/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Trouble with required fields in forms/email

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.

to post a comment
PHP

19 Comments(s)

Copy linkTweet thisAlerts:
@KravvitzFeb 13.2006 — 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.
Copy linkTweet thisAlerts:
@NogDogFeb 13.2006 — [code=php]
// 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...
[/code]
Copy linkTweet thisAlerts:
@sheepo-designsauthorFeb 14.2006 — Where do I put that? In the Head?
Copy linkTweet thisAlerts:
@sridhar_423Feb 14.2006 — thts PHP, a server-side language..

put it at the top of your page preferably as there is a header.
Copy linkTweet thisAlerts:
@sridhar_423Feb 14.2006 — 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.
Copy linkTweet thisAlerts:
@NogDogFeb 14.2006 — Yes, it needs to go at the beginning of the file (between <?php ?> tags) [i]before[/i] anything that would output to the browser (including before any HTML code, or even blank lines or spaces not inside of <?php ?> tags).
Copy linkTweet thisAlerts:
@sheepo-designsauthorFeb 14.2006 — oooooooh ok. I'll give it a shot.
Copy linkTweet thisAlerts:
@sheepo-designsauthorFeb 14.2006 — Still not working, here's the source code, maybe it'll help
[code=html]
<?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="[email protected]" />
<input name="recipients" type="HIDDEN" value="[email protected],[email protected]" />

<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="[email protected]" />
<input name="recipients" type="HIDDEN" value="[email protected],[email protected]" />

<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:[email protected]" 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>[/code]
Copy linkTweet thisAlerts:
@sheepo-designsauthorFeb 14.2006 — 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.[/QUOTE]

How would this be done?
Copy linkTweet thisAlerts:
@sridhar_423Feb 14.2006 — <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>
Copy linkTweet thisAlerts:
@sridhar_423Feb 14.2006 — put the <Script language.....

</script> inside the <HEAD> ..</HEAD>
Copy linkTweet thisAlerts:
@NogDogFeb 14.2006 — You still need to to server-side validation to handle the 10% or so of users who do not have JavaScript available/enabled.
Copy linkTweet thisAlerts:
@sheepo-designsauthorFeb 14.2006 — It worked! Thank you sridhar!
Copy linkTweet thisAlerts:
@NogDogFeb 14.2006 — 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:
<i>
</i>if(!isset($_POST[$value]) or trim($_POST[$value][color=red])[/color] == "" )
Copy linkTweet thisAlerts:
@sheepo-designsauthorFeb 14.2006 — 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.
Copy linkTweet thisAlerts:
@NogDogFeb 14.2006 — 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.[/QUOTE]
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. ? )
Copy linkTweet thisAlerts:
@sheepo-designsauthorFeb 14.2006 — 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?
Copy linkTweet thisAlerts:
@NogDogFeb 14.2006 — 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.
Copy linkTweet thisAlerts:
@sheepo-designsauthorFeb 14.2006 — excellent ? Thanks a lot NogDog
×

Success!

Help @sheepo-designs spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...