PHP Form Help

Status
Not open for further replies.

zandre88

In Runtime
Messages
241
Location
Cincinnati, OH
Hey everyone,

I am having a lot of trouble figuring something out that I'm sure is simple but I can't seem to get figured out.

I have a form set-up on the front page of a website I've been working on. It is from a template package I purchased. Anyways, the problem is I cannot get the content from the form to get sent to my e-mail the way I want it to. It is split into three different .php files. Here are each of them:

contacthome.php

PHP:
<?php
/*
Credits: Bit Repository
URL: [url=http://www.bitrepository.com/]Bit Repository | Weekly Web Resources for Programmers and Designers[/url]
*/

include 'config.php';

error_reporting (E_ALL ^ E_NOTICE);

$post = (!empty($_POST)) ? true : false;

if($post)
{
include 'functions.php';

$name = stripslashes($_POST['name']);
$message = stripslashes($_POST['phone']);
$message = trim($_POST['email']);
$message = stripslashes($_POST['address']);
$message = stripslashes($_POST['zip']);
$subject = "Estimate Request";
$message = stripslashes($_POST['message']);


$error = '';

// Check name

if(!$name)
{
$error .= 'Please enter your name.<br />';
}

// Check phone

if(!$phone)
{
$error .= 'Please enter your phone number.<br />';
}

// Check email

if(!$email)
{
$error .= 'Please enter an e-mail address.<br />';
}

if($email && !ValidateEmail($email))
{
$error .= 'Please enter a valid e-mail address.<br />';
}

// Check address

if(!$address)
{
$error .= 'Please enter your address.<br />';
}

// Check zip

if(!$zip)
{
$error .= 'Please enter your zip.<br />';
}

// Check message (length)

if(!$message || strlen($message) < 15)
{
$error .= "Please enter your message. It should have at least 15 characters.<br />";
}


if(!$error)
{
$mail = mail(WEBMASTER_EMAIL, $subject, $message,
     "From: ".$name." <".$email.">\r\n"
    ."Reply-To: ".$email."\r\n"
    ."X-Mailer: PHP/" . phpversion());


if($mail)
{
echo 'OK';
}

}
else
{
echo '<div class="notification_error">'.$error.'</div>';
}

}
?>


functions.php
PHP:
<?php
function ValidateEmail($email)
{
/*
(Name) Letters, Numbers, Dots, Hyphens and Underscores
(@ sign)
(Domain) (with possible subdomain(s) ).
Contains only letters, numbers, dots and hyphens (up to 255 characters)
(. sign)
(Extension) Letters only (up to 10 (can be increased in the future) characters)
*/

$regex = '/([a-z0-9_.-]+)'. # name

'@'. # at

'([a-z0-9.-]+){2,255}'. # domain & possibly subdomains

'.'. # period

'([a-z]+){2,10}/i'; # domain extension 

if($email == '') { 
	return false;
}
else {
$eregi = preg_replace($regex, '', $email);
}

return empty($eregi) ? true : false;
}
?>

config.php​
PHP:
<?php
// To
define("WEBMASTER_EMAIL", 'myemail@myemail.com');
?>

I have searched and tried a ton of different options but cannot get it to do what I want. When I fill out the form to test it the only thing I get in the content of the e-mail is the "Message" input. I want everything else included within the content of the e-mail.

If anyone could help me and let me know what I need to do or am doing wrong to accomplish that I'd hugely appreciate it. I know it is broken up somewhat unnecessarily but I think that was to make it easier on the person using the template. However, I just basically took the template as a base and changed a lot of it.

Anyways, any help would be awesome. If you need to see more information please let me know.

Thanks.
 
Moved to Programming.

At the top of your code in contacthome.php you're using "$message =". Aside from the initial set, use "$message .=" to concatenate the strings.
 
Moved to Programming.

At the top of your code in contacthome.php you're using "$message =". Aside from the initial set, use "$message .=" to concatenate the strings.

Thanks. Alright, that worked.

Now next question. How would I get it to be separated? Here is how the message came thru...

This is a test of the test.555-555-5555MyEmail@myemail.com1111 Special Lane45555This is a test of the test.
 
In an email you can use normal line breaks to split the lines. Add " . '\n'" to the ends of each $_POST value.
 
In an email you can use normal line breaks to split the lines. Add " . '\n'" to the ends of each $_POST value.

Thanks. After I played around with it a little while ended up figuring that out. Had never worked with PHP before this so had been flying blind but slowly starting to get it a bit.

Have two more quick questions.

First, after adding the " . '\n'" to the end of each of the values for some reason the 'phone' value isn't coming thru? Any reason why that would be happening? I'll leave the contacthome.php below.

Second, I'd like to add headers I guess you can say for each value. Here is an example of what I'd like it to look after they submit it and I get it in my inbox...

Name: John Doe
Phone: 555-555-5555
e-Mail: JohnDoe@myemail.com
Address: 5555 Example Street
Zip: 454545
Message: Blah, blah, blah, blah.

Here is what the code looks like after making the changes so far...

PHP:
<?php
/*
Credits: Bit Repository
URL: [url=http://www.bitrepository.com/]Bit Repository | Weekly Web Resources for Programmers and Designers[/url]
*/

include 'config.php';

error_reporting (E_ALL ^ E_NOTICE);

$post = (!empty($_POST)) ? true : false;

if($post)
{
include 'functions.php';

$message .= stripslashes($_POST['name'])."\n";
$message .= stripslashes($_POST['phone'])."\n";
$message = trim($_POST['email'])."\n";
$message .= stripslashes($_POST['address'])."\n";
$message .= stripslashes($_POST['zip'])."\n";
$subject .= "Estimate Request";
$message .= stripslashes($_POST['message']."\n");


$error = '';

// Check name

if(!$name)
{
$error .= 'Please enter your name.<br />';
}

// Check phone

if(!$phone)
{
$error .= 'Please enter your phone number.<br />';
}

// Check email

if(!$email)
{
$error .= 'Please enter an e-mail address.<br />';
}

if($email && !ValidateEmail($email))
{
$error .= 'Please enter a valid e-mail address.<br />';
}

// Check address

if(!$address)
{
$error .= 'Please enter your address.<br />';
}

// Check zip

if(!$zip)
{
$error .= 'Please enter your zip.<br />';
}

// Check message (length)

if(!$message || strlen($message) < 15)
{
$error .= "Please enter your message. It should have at least 15 characters.<br />";
}


if(!$error)
{
$mail = mail(WEBMASTER_EMAIL, $subject, $message,
     "From: ".$name." <".$email.">\r\n"
    ."Reply-To: ".$email."\r\n"
    ."X-Mailer: PHP/" . phpversion());


if($mail)
{
echo 'OK';
}

}
else
{
echo '<div class="notification_error">'.$error.'</div>';
}

}
?>

Thanks very much for the help Baez. I really appreciate it.
 
I modified the code. I haven't tested but this should work:

PHP:
<?php

include 'config.php';

error_reporting (E_ALL ^ E_NOTICE);

$post = (!empty($_POST)) ? true : false;

if($post)
{
include 'functions.php';

$message = 'Name: ' . stripslashes($_POST['name'])."\n";
$message .= 'Phone: ' . stripslashes($_POST['phone'])."\n";
$message .= 'Email: ' . trim($_POST['email'])."\n";
$message .= 'Address: ' . stripslashes($_POST['address'])."\n";
$message .= 'Postal/Zip: ' . stripslashes($_POST['zip'])."\n";
$message .= 'Message: ' . stripslashes($_POST['message']."\n");
$subject .= "Estimate Request";

$error = '';

// Check name

if(!$name)
{
$error .= 'Please enter your name.<br />';
}

// Check phone

if(!$phone)
{
$error .= 'Please enter your phone number.<br />';
}

// Check email

if(!$email)
{
$error .= 'Please enter an e-mail address.<br />';
}

if($email && !ValidateEmail($email))
{
$error .= 'Please enter a valid e-mail address.<br />';
}

// Check address

if(!$address)
{
$error .= 'Please enter your address.<br />';
}

// Check zip

if(!$zip)
{
$error .= 'Please enter your zip.<br />';
}

// Check message (length)

if(!$message || strlen($message) < 15)
{
$error .= "Please enter your message. It should have at least 15 characters.<br />";
}


if(!$error)
{
$mail = mail(WEBMASTER_EMAIL, $subject, $message,
     "From: ".$name." <".$email.">\r\n"
    ."Reply-To: ".$email."\r\n"
    ."X-Mailer: PHP/" . phpversion());


if($mail)
{
echo 'OK';
}

}
else
{
echo '<div class="notification_error">'.$error.'</div>';
}

}
?>
 
Status
Not open for further replies.
Back
Top Bottom