Current location: Hot Scripts Forums » Programming Languages » PHP » Help with required fields in form


Help with required fields in form

Reply
  #1 (permalink)  
Old 06-29-05, 04:23 PM
Pixelpyro Pixelpyro is offline
Newbie Coder
 
Join Date: Apr 2005
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Help with required fields in 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-29-05, 11:57 PM
snaip's Avatar
snaip snaip is offline
iNET Interactive
 
Join Date: Nov 2003
Posts: 998
Thanks: 2
Thanked 7 Times in 6 Posts
Moving to PHP Forums
__________________
Regards,
Ahmad Permessur
Team HotScripts
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, 12:19 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
try some error checking before sending the mail, ie:
PHP Code:

<?php


if (trim($_POST['name'])=='' OR
    
trim($_POST['email'])=='' OR
    
trim($_POST['phone'])=='' OR
    
trim($_POST['message'])=='') {

$errors='';

    if (
trim($_POST['name'])=='')
    
$errors.='<li>No name.</li>';

    if (
trim($_POST['email'])=='')
    
$errors.='<li>No email address.</li>';

    if (
trim($_POST['phone'])=='')
    
$errors.='<li>No phone.</li>';

    if (
trim($_POST['message'])=='')
    
$errors.='<li>No message.</li>';

echo 
'Errors were encountered:';

echo 
'<ul>';
echo 
$errors;
echo 
'</ul>';

}
else {

// All fields were submitted and you can mail();

}

?>

Last edited by Keith; 06-30-05 at 12:28 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
Required fields in a quick form Pixelpyro PHP 2 06-30-05 10:03 AM
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 06:42 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.