getting closer, but still having some problems with form

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
bruceg
Forum Contributor
Posts: 174
Joined: Wed Mar 16, 2005 11:07 am
Location: Morrisville, NC
Contact:

getting closer, but still having some problems with form

Post by bruceg »

Hello,

on my web site contact form:

http://www.inspired-evolution.com/Contact.php

I am still having a few problems with the return results after filling out the form. Basically I am wanted to return an error msg. when all of the required fields are not filled out (those with a red *), and an invalid email address will also return an error.

Right now even if you don't fill out the required fields, you still get my thank-you message for filling out the form correctly (as well as getting the error msg.). If someone has a chance try out the form yourself and you will see what I mean.

anyway, the validation code I currently have is:

Code: Select all

<?php
 
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$company = $_POST['company'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$email2 = $_POST['email2'];
$URL = $_POST['URL'];
$Contact_Preference = $_POST['Contact_Preference'];
$Contact_Time = $_POST['Contact_Time'];
$message = $_POST['Message'];
 
if ((!$firstname) || (!$Contact_Preference)) {
    echo'<p><strong>Error!</strong> Fields marked <span class="red"> *</span> are required to continue.</p><br />';

}
 
if (!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*"."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email)) {
    echo '<p>Invalid email address entered. Please <a href="Contact.php" title="Contact Me">Try again.</a></p><br />';

}
 
$email_address = "webguync@gmail.com";
$subject = "There has been a disturbance in the force";
 
$message = "Request from: $firstname $lastname\n\n
Company name: $company\n
Phone Number:  $phone\n
Email Address: $email\n
URL: $URL\n
Please Contact me via: $Contact_Preference\n
The best time to reach me is: $Contact_Time\n
I wish to request the following additional information: $Textarea";
 
mail($email_address, $subject, $message, "From: $email \nX-Mailer: PHP/" . phpversion());
 
echo "<p>Hello, <strong>$firstname</strong>.<br /><br />
We have received your request for additional information, and will respond shortly.<br />
Thanks for visiting inspired-evolution.com and have a wonderful day!<br /><br />
Regards,<br /><br />
<strong>Inspired Evolution</strong></p>";

?>
User avatar
IceMetalPunk
Forum Commoner
Posts: 71
Joined: Thu Jul 07, 2005 11:45 am

Post by IceMetalPunk »

When you output the error message, use exit() instead of echo. For example, do this:

Code: Select all

exit("Invalid E-Mail.");
Instead of this:

Code: Select all

echo "Invalid E-Mail";
This way, the rest of the script (including the e-mail and thank you message) are not output.

-IMP ;) :)
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hi,

you could use return to hault the script upon error . ie:

Code: Select all

if ((!$firstname) || (!$Contact_Preference)) {

	echo'<p><strong>Error!</strong> Fields marked <span class="red"> *</span> are required to continue.</p><br />';
	require 'contact.php';
    return;
} 
	
if (!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*"."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email)) {

	echo '<p>Invalid email address entered.</p><br />';
	require 'contact.php';
    return;
}
You should also echo the $POST data in the value="" of the textarea so when the form reloads upon error the typed data isnt lost .. ie:

Code: Select all

<input name="firstname" type="text" value="<?php echo $_POST['firstname']; ?>">

and you have no error checking on the email + retype email fields ..

hth
bruceg
Forum Contributor
Posts: 174
Joined: Wed Mar 16, 2005 11:07 am
Location: Morrisville, NC
Contact:

Post by bruceg »

Thanks for the suggestion, there Jimbo. I have added the code you suggested,

but now I am getting a double page being returned (two headers, two bodies etc.)

http://www.inspired-evolution.com/Contact.php

http://www.inspired-evolution.com/Thankyou.php

I guess one solution might be o create a Contact2.php page to require when there is an error that doesn't have the header and footer information, or is there a better way? (there probably is) .

also, what would error checking on the email fields need to conist of? I though I had that with this part:

Code: Select all

if (!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*"."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email)) {
 
    echo '<p>Invalid email address entered.</p><br />';
    require 'Contact.php';
    return;
}
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hi,
By email check I mean make sure that fields email & email2 match ie:

Code: Select all

if (($email) != ($email2)) {
 
	echo '<b>Error!</b> Email addresses dont match.<br>';
	include 'contact.php'; 
	return;

}
As for your header footer problem, im not sure of your layout.

Are you using index.php to hold your switches, and calling header and footer into it?
bruceg
Forum Contributor
Posts: 174
Joined: Wed Mar 16, 2005 11:07 am
Location: Morrisville, NC
Contact:

Post by bruceg »

for the header and footer I am using require:

for the header:

Code: Select all

<div id=\"header\"><?php require('images/Rotating_Banner/rotating_banner.php'); ?></div>
</php>

and for the footer:

[php]
<div id=\"footer\">
<?php require('includes/footer.inc'); ?>
<span class=\"date\"><?
$last_modified = filemtime(\"index.php\");
print(\"Last Modified \");
print(date(\"m/j/y h:i\", $last_modified));
?></span>
</div>
[/php]

... and that code is on every page.
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hi,

I code differently to that, maybe take out the require 'contact.php'; code then and just link back to the contact page as you were.
bruceg
Forum Contributor
Posts: 174
Joined: Wed Mar 16, 2005 11:07 am
Location: Morrisville, NC
Contact:

Form submission problems-almost there!

Post by bruceg »

Thanks for all of the assistance so far. Still having some issues.

not filling out the form correctly gives you an error msg. but also displays the tahnk-you message. I tried replacing echo with exit as per the earlier suggestion, but that gives me this error message.

Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING in /hsphere/local/home/bruceg/inspired-evolution.com/Thankyou.php on line 43


what I really want to accomplish is to have a thank-you page for succussfull completion, and an ooops! page when there is an error. Two different pages alltogether based upon the results of the form.

My current PHP code for the submission is:

Code: Select all

<?php
 
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$company = $_POST['company'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$email2 = $_POST['email2'];
$URL = $_POST['URL'];
$Contact_Preference = $_POST['Contact_Preference'];
$Contact_Time = $_POST['Contact_Time'];
$message = $_POST['Message'];
 
if ((!$firstname) || (!$Contact_Preference)) {
 
    echo'<p><strong>Error!</strong> Fields marked <span class="red"> *</span> are required to continue.</p><br />';
echo'<p>Please go back to the <a href="Contact.php"title="Contact Me">Contact Me</a> page and try it again!</p>';
} 
    
if (!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*"."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email)) {
 
    echo '<p>Invalid email address entered.</p><br />';
echo '<p>Please go back to the <a href="Contact.php" title="Contact Me">Contact Me</a> page and try it again!</p>';
}
if (($email) != ($email2)) {
 
    echo '<strong>Error!</strong> e-mail addresses dont match.<br />';

 
}
 
$email_address = "webguync@gmail.com";
$subject = "There has been a disturbance in the force";
 
$message = "Request from: $firstname $lastname\n\n
Company name: $company\n
Phone Number:  $phone\n
Email Address: $email\n
URL: $URL\n
Please Contact me via: $Contact_Preference\n
The best time to reach me is: $Contact_Time\n
I wish to request the following additional information: $Textarea";
 
mail($email_address, $subject, $message, "From: $email \nX-Mailer: PHP/" . phpversion());
 
echo "<p>Hello, <strong>$firstname</strong>.<br /><br />
We have received your request for additional information, and will respond shortly.<br />
Thanks for visiting inspired-evolution.com and have a wonderful day!<br /><br />
Regards,<br /><br />
<strong>Inspired Evolution</strong></p>";

?>

any additional assistance is greatly appreciated!

URL for the form again is http://www.inspired-evolution.com/Contact.php
Post Reply