[SOLVED] PHP Contact Form

This is a discussion on "[SOLVED] PHP Contact Form" within the PHP Forum section. This forum, and the thread "[SOLVED] PHP Contact Form are both part of the Program Your Website category.



 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Closed Thread
 
LinkBack Thread Tools
  #1  
Old Dec 4th, 2007, 16:51
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] PHP Contact Form

Hello everyone,

I have a small problem with the contact form on my website. When the form is submitted, and there was something wrong with something the user entered into the form, the label for that field changes color. Here is a visual:

No error
Label: [textbox]

Error
Label: [textbox]

Now, that part works great. The problem is that when the form is redisplayed because there was something wrong, the stuff that was entered should still be there so the user knows what they typed and can see if they made a mistake. My entire code is as follows:
PHP: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Form</title>
<style type="text/css">
body {font-family:Verdana, Arial, Helvetica, sans-serif;font-size:12px;}
.heading, .error {color:#f00;font-weight:bold;}
</style>
</head>

<body>
<?php

if (isset($_POST['submit'])){ // check if form has been submitted, it if has do the processing

$name $_POST['name'];
$email $_POST['email'];
$type $_POST['type'];
$prblm1 $_POST['problem1'];
$prblm2 $_POST['problem2'];
$prblm3 $_POST['problem3'];
$other $_POST['other'];
$url $_POST['url'];
$message $_POST['message'];
$ip $_POST['ip'];
$browser $_POST['browser'];

// start an empty array
$errors = array();
$errorBits = array();

if (
eregi('http:'$message)){
    
$errors[] = '<p class="heading">Please enter the URL into the URL field</p>';
    
$errorBits['url'] = true;
    }

if(!empty(
$email) && !eregi("^[_a-z0-9-]+(\.[_a-z0-9-]*)*@[a-z0-9-]+(\.[a-z0-9-]+)+$",$email)){
    
$errors[] = '<p class="heading">Invalid email address entered</p>';
    
$errorBits['email'] = true;
    }

if(empty(
$email) || empty($message)){
    
$errors[] = '<p class="heading">Please fill in all required fields</p>';
    
$errorBits['email'] = true;
    
$errorBits['message'] = true;
    }

if(isset(
$prblm3) && empty($other)) {
    
$errors[] = '<p class="heading">You have selected "Other" under "Problem". Please specify</p>';
    
$errorBits['other'] = true;
    }

// Check for errors before sending email
if (!count($errors)){
    
$today date("l, F j, Y, g:i a");    
    
$subject "Contact the Webmaster";
    
$message stripcslashes($message);

    
$message "Sent: $today [EST] \n
From: $name ($email) \n
Type: $type; $prblm1 $prblm2 $prblm3 $other \n
URL: $url \n
Message: $message \n
User IP: = $ip \n
Browser Info: $browser \n"
;

    
$from "From: $email\r\n";

    
mail("my.email@yahoo.com"$subject$message$from);

    echo 
'<p align="center" class="heading">Your message has been submitted successfully...</p>
    <p align="center" class="description">Thank you for helping us improve our website</p>'
;
    }

else {
    
$errorMessage ''// set up an error message

    // now output the errors for user to see
    
foreach ($errors as $msg
    echo 
$msg;
    }
}

// the form processing out of the way if it was required
if (!isset($_POST['submit']) || isset($errorMessage) || (isset($errorBits) && count($errorBits))){
// if the form hasn't been submitted or it HAS and there have been errors, we will write the form

    
if (isset($errorMessage)){
        echo 
$errorMessage;
        }

?>
<form method="post" action="webmaster2.php" name="wbmstr" enctype="multipart/form-data">
Name: <input type="text" name="name" size="35" value=""><br>
<label for="email"<?php if($errorBits['email'] == true){echo ' class="error"';} ?>>* Email:</label> <input type="text" name="email" id="email" size="35" value=""><br>
<label<?php if($errorBits['type'] == true || $errorBits['prblm3'] == true){echo ' class="error"';} ?>>* Message Type:</label><br>
<input type="radio" name="type" id="type1" value="problem" checked="checked"> Problem<br>
<input type="checkbox" name="problem1" id="prblm1" value="text"> Text<br>
<input type="checkbox" name="problem2" id="prblm2" value="images"> Images<br>
<input type="checkbox" name="problem3" id="prblm3" value="other:"> <label<?php if($errorBits['other'] == true){echo ' class="error"';} ?>>Other:</label> <input type="text" name="other" value=""><br>
<input type="radio" name="type" id="type2" value="comment/question"> Comment or Question<br>
<input type="radio" name="type" id="type3" value="suggestion"> Suggestion<br>
<label>URL:</label> <input type="text" name="url" id="url" size="45" value="http://"><br>
<label<?php if($errorBits['message'] == true || $errorBits['url'] == true){echo ' class="error"';} ?>>* Message:</label><br><textarea name="message" rows="10" cols="45"></textarea><br>
<input type="submit" value="submit" name="submit" class="button">
</form>
<?php

?>
</body>
</html>
I thought I could use the defined variables at the beginning of the php ($name = $_POST['name']; $email = $_POST['email']; ...) and put that into every input tag's value. Like this:
HTML: Select all
<input type="text" name="name" value="<php echo $name ?>">
I'm sure that there's a much better method though... Any suggestions?

Thanks
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!

  #2  
Old Dec 4th, 2007, 16:58
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Contact Form

You needn't worry about adding the $_POST variables to $variables, just do a check within each and write straight from $_POST

eg

value="<?php echo isset($_POST['name']) ? $_POST['name'] : ''; ?>
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #3  
Old Dec 4th, 2007, 17:06
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Contact Form

Hmm... It works, yes, but I was wondering if there is one code that can do this for every field without having to add php into the value.
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #4  
Old Dec 4th, 2007, 17:11
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Contact Form

Nope.

Unless you write the entire form with PHP, ie echo everything then you will have to intermingle the code, php writes the HTML it doesn't interact with it.

Cheers
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #5  
Old Dec 4th, 2007, 17:12
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Contact Form

Using templates could maybe simplify your problem....
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #6  
Old Dec 4th, 2007, 17:13
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Contact Form

Templates is not something I want to use on this particular site. Luke's suggestion works fine so I'll use that.
Thanks for the help!
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)

Last edited by Stuart; Dec 4th, 2007 at 17:18.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #7  
Old Dec 4th, 2007, 17:19
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Contact Form

Actually, another small question. Luke, the code you posted works for the textfields, but what about radio buttons and checkboxes? Can't seem to figure it out...
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #8  
Old Dec 4th, 2007, 17:31
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Contact Form

inside them use

<?php echo $_POST['what'] == 'this_value' ? ' selected="selected" ' : ''; ?>

replacing selected with checked for radios etc..

So with those, you're checking agains the value that option of radio holds
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #9  
Old Dec 4th, 2007, 17:42
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Contact Form

OK, great! It works perfectly! Thanks for all your help

Thread Solved
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Closed Thread

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] PHP contact form problem fl0w PHP Forum 12 Jan 31st, 2008 17:56
[SOLVED] PHP contact form redirect to same form Posie PHP Forum 14 Jan 29th, 2008 20:28
[SOLVED] Wordpress contact form - getting 404 on send Aso PHP Forum 3 Jan 25th, 2008 11:23
[SOLVED] Contact Form Help danny322 PHP Forum 3 Nov 7th, 2007 16:05
[SOLVED] PHP Contact Form Stuart PHP Forum 9 Oct 20th, 2007 00:48


All times are GMT. The time now is 09:56.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42