Current location: Hot Scripts Forums » Programming Languages » PHP » Required fields in a quick form


Required fields in a quick form

Reply
  #1 (permalink)  
Old 06-29-05, 09:22 AM
Pixelpyro Pixelpyro is offline
Newbie Coder
 
Join Date: Apr 2005
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Required fields in a quick form

Hi Guys,

I have the following form that I use a quick solution for a contact form - I like it as its all one the single page (even the confirmation) but I would like to have some required fields such as name and email and a message that appears (such as the Thank You) if those field aren't filled in.

How would I do this? My php skills are still at a newbie level and although I can see what is happening (with the code - I don't have the knowledge to go that one step further). Thanks for your time, it is very much appreciated

Code:
<?php
if ($_POST) {
if (get_magic_quotes_gpc()) {
foreach ($_POST as $key => $value) {
$temp = stripslashes($value);
$_POST[$key] = $temp;
}
}
$to = 'info@domain.com';
$subject = 'CEC website contact form';
// message goes here
$message = "Full Name: " . $_POST['name'] . "\n";
$message .= "Email: " . $_POST['email'] . "\n";
$message .= "Telephone: " . $_POST['phone'] . "\n";
	$message .= "Message: " . $_POST['message'] . "\n";

// headers go here
$headers = "From: " . $_POST['email'] . "\n";
$headers .= "Content-type: text/plain; charset=UTF-8";

$sent = mail($to, $subject, $message, $headers);
$autorespond = mail($_POST['email'], "Form Auto Response.", "Thank you for contacting me, your message has been received and we will contact you shortly.", "From: Me<info@domain.com>");
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">

<head>
<title>Title</title>
<meta http-equiv="Content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>

<h1>Contact</h1>

<?php
if (isset($sent)) {
echo '<p><span class="thanks"><strong>Thank you</strong> for contacting us. Your message will be responded to as soon as possible.</span></p>';
}
?>

<div id="form">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<fieldset>
<legend>Personal information</legend>

<span class="formLabel"><label for="name">Full Name:</label><br /></span>
<input class="inputfield" type="text" id="name" name="name" size="30" />

<span class="formLabel"><label for="email">Email:</label><br /></span>
<input class="inputfield" type="text" id="email" name="email" size="30" />

<p><span class="formMessage"><label for="message">Message:</label><br /></span>
<textarea class="inputarea" id="message" name="message" rows="5" cols="30"></textarea></p>

</fieldset>

<p><input type="submit" class="inputsend" name="Submit" value="Send Form" /></p>

</form></div><!-- end #form -->
</body>
</html>
Thank You.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 06-30-05, 06:31 AM
Vulture Vulture is offline
Newbie Coder
 
Join Date: Dec 2004
Location: Scotland, UK
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
There are numerous ways to check for required fields. Your also not limited to PHP, JavaScript can perform these checks before the form is submitted. However, here is one way you could do it:
PHP Code:

<?php

if (($_POST) and (!empty($_POST['name'])) and (!empty($_POST['email'])) and (!empty($_POST['message']))) {
    if (
get_magic_quotes_gpc()) {
        foreach (
$_POST as $key => $value) {
            
$temp stripslashes($value);
            
$_POST[$key] = $temp;
        }
    }
    
$to 'info@domain.com';
    
$subject 'CEC website contact form';

    
// message goes here
    
$message "Full Name: " $_POST['name'] . "\n";
    
$message .= "Email: " $_POST['email'] . "\n";
    
$message .= "Telephone: " $_POST['phone'] . "\n";
    
$message .= "Message: " $_POST['message'] . "\n";

    
// headers go here
    
$headers "From: " $_POST['email'] . "\n";
    
$headers .= "Content-type: text/plain; charset=UTF-8";

    
//send mail
    
$sent mail($to$subject$message$headers);
    
$autorespond mail($_POST['email'], "Form Auto Response.""Thank you for contacting me, your message has been received and we will contact you shortly.""From: Me <info@domain.com>");
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">

<head>
<title>Title</title>
<meta http-equiv="Content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>

<h1>Contact</h1>

<?php
if (isset($sent)) {
    echo 
'<p><span class="thanks"><strong>Thank you</strong> for contacting us. Your message will be responded to as soon as possible.</span></p>';
}
else if ((empty(
$_POST['name'])) or (empty($_POST['email'])) or (empty($_POST['message']))) {
    echo 
'<p>Please ensure you have filled out all the required fields</p>';
}
?>

<div id="form">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<fieldset>
<legend>Personal information</legend>

<span class="formLabel"><label for="name">Full Name:</label><br /></span>
<input class="inputfield" type="text" id="name" name="name" size="30" />

<span class="formLabel"><label for="email">Email:</label><br /></span>
<input class="inputfield" type="text" id="email" name="email" size="30" />

<p><span class="formMessage"><label for="message">Message:</label><br /></span>
<textarea class="inputarea" id="message" name="message" rows="5" cols="30"></textarea></p>

</fieldset>

<p><input type="submit" class="inputsend" name="Submit" value="Send Form" /></p>

</form></div><!-- end #form -->
</body>
</html>
The changes I've made are pretty straight forward. At the first if statement I've added on a check to see whether the fields are empty or not. If they are, the entire send mail block is skipped. I then perform a similar check in the later part of the code, which outputs an error if any of these fields are empty.

Hope that helps
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 06-30-05, 10:03 AM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
http://www.programmingtalk.com/showthread.php?t=20217

How about you stick to one topic rather than copying and re-posting your other topic?

Last edited by Keith; 06-30-05 at 10:05 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to make read-only form fields Ashantai JavaScript 4 02-15-05 05:26 PM
I most definately suggest DevelopingCentral.com For Any Website Design/Development! Salty777 General Advertisements 2 10-01-04 05:27 AM
formmail problem gscraper Perl 12 08-27-04 04:06 AM
Form validation - required fields Funky_Monk JavaScript 1 08-16-04 11:26 AM
searching from a form where some fields are not required bobdole PHP 1 12-08-03 05:14 AM


All times are GMT -5. The time now is 01:04 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.