Making My Form Fields Required

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
peschomd
Forum Newbie
Posts: 3
Joined: Wed Aug 15, 2007 4:07 pm

Making My Form Fields Required

Post by peschomd »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi everyone, I'm new here.

I am trying to make the form fields "First name, last name, state, phone, and email" required.

the page is here 

http://www.mankatowebdesign.com/SMS/cen ... egion.html

The PHP Code I have currently my friend helped me so I know the (if) statements need to be fixed. If anyone could explain what I need to change the if's to to fix this it would help.

Code: Select all

<?php


$txtCompanyName =  $_POST["txtCompanyName"];
$txtFirstName =  $_POST["txtFirstName"];
$txtLastName =  $_POST["txtLastName"];
$txtTitle =  $_POST["txtTitle"];
$txtStreetAddress =  $_POST["txtStreetAddress"];
$txtCity =  $_POST["txtCity"];
$txtState =  $_POST["txtState"];
$txtZip =  $_POST["txtZip"];
$txtCountry =  $_POST["txtCountry"];
$txtPhone =  $_POST["txtPhone"];
$txtEmailAddress =  $_POST["txtEmailAddress"];
$txtNumberOfEmployees =  $_POST["txtNumberOfEmployees"];
$chkContactMeChoicesA = $_POST["ChkContactMeChoicesA"];
$chkContactMeChoicesB = $_POST["ChkContactMeChoicesB"];
$chkContactMeChoicesC = $_POST["ChkContactMeChoicesC"];
$chkContactMeChoicesD = $_POST["ChkContactMeChoicesD"];
$txtContactMeChoicesD1 = $_POST["txtContactMeChoicesD1"];
$txtHearAboutUs = $_POST["HearAboutUs"];
$txtHearAboutUsOther = $_POST["txtHearAboutUsOther"];
$txtComments = $_POST["txtComments"];



	


	if($txtHearAboutUs == "No Selection" || $txtRegionalManager == "No Selection")
	{
		echo "<h2>One or more required fields are missing. Please hit 'back' on your browser and fill in all required fields.</h2>\n";
	}
	
	$ContactMeChoiceFormatted = "";
	
	if(!empty($chkContactMeChoicesA))
	{
		$ContactMeChoiceFormatted = $chkContactMeChoicesA;
	}
	if(!empty($chkContactMeChoicesB))
	{
		$ContactMeChoiceFormatted = $ContactMeChoiceFormatted .", ". $chkContactMeChoicesB;
	}
	if(!empty($chkContactMeChoicesC))
	{
		$ContactMeChoiceFormatted = $ContactMeChoiceFormatted .", ". $chkContactMeChoicesC;
	}
	if(!empty($chkContactMeChoicesD))
	{
		$ContactMeChoiceFormatted = $ContactMeChoiceFormatted .", ". $chkContactMeChoicesD . $txtContactMeChoicesD1;
	}
	
	
	
	

$todayis = date("l, F j, Y, g:i a") ;

$message = " $todayis [EST] \n

Company: $txtCompanyName
----------------------------------- 
From: $txtLastName, $txtFirstName ($txtEmailAddress)
-----------------------------------
Phone: $txtPhone
-----------------------------------
Title: $txtTitle
-----------------------------------
Address: 

$txtStreetAddress

$txtCity, $txtState, $txtZip, 

$txtCountry
-----------------------------------
Number Of Employees: $txtNumberOfEmployees
-----------------------------------
Please Contact Me Regarding: $ContactMeChoiceFormatted
-----------------------------------	
How Did I Hear About SMS: $txtHearAboutUs
-----------------------------------
If Other: $txtHearAboutUsOther
-----------------------------------
Comments: $txtComments
-----------------------------------

";
	
mail("ddooley@scholarshipamerica.org", "SMS Web Request", $message, $txtEmailAddress);
	


?>




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="keywords" content="tuition assistance,loan programs,scholarship management,sms,scholarship america">
<meta name="description" content="Scholarship Management Services provides scholarship management, tuition assistance, loan programs, and case studies about our clients.">
<link type="text/css" href="http://www.mankatowebdesign.com/SMS/style.css" rel="stylesheet" media="screen" />
<title>Thank You For Your Message</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="shortcut icon" href="favicon.ico" >
</head>
<body>

<div id="container">
<div class="navigation">
  <a href="contact.html">Contact</a> | <a href="faq.html">FAQ</a> | <a href="newsletter.html">Newsletter</a> | <a href="refer_this_site.html">Refer This Site</a></div>
	<div id="header" >
	<img border="0" src="services.gif"></div>
<div id="left"><div class="top_tier"> <a href="index.html">Home</a></div>
<div class="top_tier"> <a href="services.html">Services</a></div>  
<div class="bottom_tier1"> <a href="scholarships.html">Scholarships</a></div>  
<div class="bottom_tier"> <a href="tuition_assistance.html">Tuition Assistance</a></div>  
<div class="bottom_tier"> <a href="loan_programs.html">Loan Programs</a></div> 
<div class="top_tier"> <a href="sms_advantage.html">SMS Advantage</a></div> 
<div class="top_tier"> <a href="about.html">About Us</a></div> 
<div class="bottom_tier"> <a href="leadership.html">Leadership</a></div>  
<div class="bottom_tier"> <a href="job_opportunities.html">Job Opportunities</a></div> 
<div class="top_tier"> <a href="case_studies.html">Case Studies</a></div>  
<div class="top_tier"> <a href="news.html">News & Events</a></div> 
<div class="bottom_tier"> <a href="newsletter.html">Newsletter</a></div> 
<div class="top_tier"> <a target="_blank" href="http://scholarshipamerica.org/students/index.html">Student Resources</a></div> 
<div class="top_tier"> <a href="refer_this_site.html">Refer This Site</a></div> 
<div class="top_tier"> <a href="faq.html">FAQs</a></div> 
<div class="top_tier"> <a href="contact.html">Contact Us</a></div> 

<p>  
<br />
<br />
</p>
<div align="center"><a target="_blank" href="http://www.scholarshipamerica.org">
	<img src="scholarshiplogo.jpg" border="0" alt="Scholarship America" /></a></div>
</div>
<div id="right" style="width: 181px; height: 459px">
		<h2>&nbsp;</h2>
  </div>	
  <div id="content_index">
    <div class="hierarchy">
	Viewing: <a href="index.html">Home</a>   
    > <a href="contact.html">Contact Us</a> > Thank You   
    </div>

<p><font size="4" color="#A5262F"><b>Thank You For Your Message!<br>
<br>
</b></font><font size="2">Your message has been received. </font></p>
	<p>&nbsp;</p>
	<p>&nbsp;</p>
</div>
 	<div id="footer"><font size="1">Registered
 		</font><span style="font-family: Arial"><font style="font-size: 9pt"></font></span><font size="1"> 2007 Scholarship Management Services | 
		<a href="index.html">Home</a> | <a href="contact.html">Contact Us</a> | 
		<a href="sitemap.html">Sitemap</a> | <a href="privacy_policy.html">Privacy Policy</a><br />
 		</font>
 	</div>
</div>
</body>
</html>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Making My Form Fields Required

Post by superdezign »

peschomd wrote:I am trying to make the form fields "First name, last name, state, phone, and email" required.

[...]

If anyone could explain what I need to change the if's to to fix this it would help.
... You just check them before posting (which you SHOULD already have been doing, anyway).

Look at isset() and empty().
peschomd
Forum Newbie
Posts: 3
Joined: Wed Aug 15, 2007 4:07 pm

Post by peschomd »

i am really unsure what you mean, could you possibly show me what code ill need to put in the if area to make those 5 things required?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

peschomd wrote:i am really unsure what you mean, could you possibly show me what code ill need to put in the if area to make those 5 things required?
Read the documentation for the functions I posted.
peschomd
Forum Newbie
Posts: 3
Joined: Wed Aug 15, 2007 4:07 pm

Post by peschomd »

im a noob i do not understand the links
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

peschomd wrote:im a noob i do not understand the links
That's no excuse. If you can read English and know what a function is, then the documentation should be pretty clear. They even give code examples. If you're expecting me to just give you the answer to such a simple question, you're mistaken.
angelena
Forum Commoner
Posts: 53
Joined: Mon Nov 22, 2004 4:10 am

Post by angelena »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


A simpler example of form validation :

form.php
-----------

[syntax="html"]
<html>
<head></head>
<body>
<form action="handler.php" method="post">
First Name:<br /><input type="text" name="first_name"  size="20" /><br />
Email:<br /><input type="text" name="the_email" id="the_email_id" size="20" maxlength="60" /><br />
<input type="submit" name="submit_btn" value="check" />
</form>
</body>
</html>


handler.php
---------------[/syntax]

Code: Select all

<?php

$email        = (string) $_POST['the_email'];
$firstname  = (string) $_POST['first_name'];

if (empty($email))
{
       die('You did not enter anything for email. Please go <a href="javascript:history.back(-1);">back</a>.');
}
if (empty($firstname))
{
       die('You did not enter anything for first name. Please go <a href="javascript:history.back(-1);">back</a>.');
}




echo "Thank You, form successfully submitted";

?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

angelena wrote:

Code: Select all

$email        = (string) $_POST['the_email'];
$firstname  = (string) $_POST['first_name'];
FYI, you are using variables without any verification that they exist. If you turned on error_reporting, this would cause notices to be thrown by attempting to access undefined variables. This is the reason that we have isset().
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Here's angelena's simpler example of form validation filled out with most of the standard stuff:

template/register.php
-----------

Code: Select all

<html>
<head></head>
<body>
<span style="color:red"><?php echo $errmsg; ?></span>
<form action="register.php" method="post">
<input type="hidden" name="submit" value="yes" />
First Name:<br /><input type="text" name="first_name"  size="20" value="<?php echo $first_name; ?>"/><br />
Email:<br /><input type="text" name="email" id="the_email_id" size="20" maxlength="60" value="<?php echo $email; ?>"/><br />
<input type="submit" name="submit_btn" value="check" />
</form>
</body>
</html>

register_thankyou.php
-----------

Code: Select all

<html>
<head></head>
<body>
<p>Thank You, form successfully submitted</p>
</body>
</html>


register.php
---------------

Code: Select all

<?php
$errmsg = '';
$success = false;
if (isset($_POST['submit'])) {     // hidden param sent means form sent
       // filter post values to accept only characters we allow
       $email = isset($_POST['email']) ? preg_replace('/[^a-zA-Z0-9\@\.\-\_]/', '', $_POST['email']) : '';
       $firstname = isset($_POST['first_name']) ? preg_replace('/[^a-zA-Z\-\ ]/', '', $_POST['first_name']) : '';

       // validate post values
       if (empty($email)) {
              $errmsg .= 'Please enter your email. ';
       }
       if (empty($firstname)) {
              $errmsg .= 'Please enter your first name. ';
       }
       if (! $errmsg) {
              $success = true;
       } 
} else {
       $email = '';
       $firstname = '';
}

if ($success) {
       header('Location: http://www.mydomain.com/register_thankyou.php");    // redirect so user cannot resubmit
} else {
       include 'template/register.php';
}
edit: superdezign's fixes
Last edited by Christopher on Wed Aug 15, 2007 10:06 pm, edited 2 times in total.
(#10850)
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Hehe.

preg_replace calls should have 3 arguments (parse error, I think) and delimiters. Also, you don't need to escape '@' or the underscore. And in the first name... is that a.. space? I'm not sure.
'Location:' header needs to use a full URL.
Even if the form is submitted, you should still check that $_POST data isset(), since POST data can be altered.


I like the procedural separation of code approach though. Wish I thought of that back in the day. ^_^
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

I think you should start quoting my sloppy code and fixing it! ;)
(#10850)
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

arborint wrote:I think you should start quoting my sloppy code and fixing it! ;)
:P Almost did. That's how I noticed the missing commas in preg_replace.
I almost didn't read it. I told myself not to. And then, I saw it was three files, and I was compelled to read!
angelena
Forum Commoner
Posts: 53
Joined: Mon Nov 22, 2004 4:10 am

Post by angelena »

oooppsss

thanks for correcting it :D
Post Reply