Jump to content

Recommended Posts

I am still new to PHP, but I am having this issue. I created a simple contact form that just emails the owner of the site the name, email address, and comments a visitor enters. I tested the form and it work on my website host sever, witch is using PHP version 4.4.9. When he loaded onto his website host sever, witch is running PHP Version 5.2.4-2ubuntu5.6, he receives an email, but the information entered by the visitor doesn't show up. When I run it on my website server, I get all the information I enter into the form. I do know that the data is being collected by the form. I used the following code to determine that.

 

echo "<pre>";

echo "POST:";

print_r($_POST);

echo "</pre>"; 

 

I have been learning to program using PHP 5 from a book and that is where I got most of this code from.

 

<?php include('includes/title.inc.php');
  include('includes/header.php'); 
  
// Process the email
if (array_key_exists('send', $_POST)) {
$to = 'aa@email.com'; //email address data is sent too
$subject = 'Feedback';

// List expected fields
$expected = array('name', 'email', 'comments');

// Set required fields
$required = array('name', 'email', 'comments');

// Create empty array for any missing fields
$missing = array();	

echo "<pre>";
echo "POST:";
print_r($_POST);
echo "</pre>";  	

// assume that there is nothing suspect
$suspect = false;

// create a pattern to locate suspect phrases
$pattern = '/Content-Type:|Bcc:|Cc:/i';	

// function to check for suspect phrases
function isSuspect($val, $pattern, &$suspect) {
//if the variable is an array, loop through each element
//and pass it recursively back to the same function
if (is_array($val)) {
	foreach ($val as $item) {
		isSuspect($item, $pattern, $suspect);
		}
	}	
else {
	//if one of the suspect phrases is found, set Boolean to true
	if (preg_match($pattern, $val)) {
		$suspect = true;
		}
	}
}

// check the $_POST array and any subarrays for suspect content
isSuspect($_POST, $pattern, $suspect);

if ($suspect) {
$mailSent = false;
unset($missing);
}
else {	
// Process the $_POST variables
foreach ($_POST as $key => $value) {
	// assign to temprary variable and strip whitespace if not an array
	$temp = is_array($value) ? $value : trim($value);
	// if empty and required, add to $missing array
	if (empty($temp) && in_array($key, $required)) {
		array_push($missing, $key);
		}
	// otherwise, assign to a variable of the same name as $key
	elseif (in_array($key, $expected)) {
		${key} = $temp;
		}
	}
}

// validate the email address
if (!empty($email)) {
//regex to ensure no illegal charaters in emial address
$checkEmail = '/^[^@]+@[^\s\r\n\'";,@%]+$/';
//reject the email address if it doesn't match
if (!preg_match($checkEmail, $email)) {
	array_push($missing, 'email');
	}
}	

// Go ahead only not suspect if all required fields OK
if (!$suspect && empty($missing)) {	


// Build the message
	$message = "Name: $name\n\n";
	$message .= "Email: $email\n\n";
	$message .= "Comments: $comments\n\n";

// limit line lenght to 70 characters
	$message = wordwrap($message, 70);

// create additional headers
$additionalHeaders = 'From: AA <aa@email.com>';
if (!empty($email)) {
	$additionalHeaders .= "\r\nReply-To: $email";
}

//sent it
$mailSent = mail($to, $subject, $message, $additionalHeaders);
if ($mailSent) {
	// $missing is no longer needed if the email is sent, so unset it
	unset($missing);
	}
}
}	
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

</head>
<body>
<div id="outer"> <!-- The outer contenter -->
<div id="header"> <!-- The header section of the page -->
  </div>
    <div id="container">

        <h2>Contact</h2>
  		<?php 
	if ($_POST && isset($missing) && !empty($missing)) {
	?>
        	<p class="warning">Please complete the missing item(s) indicated.</p>
        <?php
	}
	elseif ($_POST && !$mailSent) {
	?>
        	<p class="warning">Sorry, there was a problem sending your message. Please try later</p>
        <?php 
         }
         elseif ($_POST && $mailSent) {
         ?>
         	<p class="message_sent"><strong>Your message has been sent. Thank you for your feedback</strong></p>
        <?php } ?>
                <form id="feedback" method="post" action="">
            <p>
                <label for="name">Name: <?php 
				if (isset($missing) && in_array('name', $missing)) { ?>
                    <span class="warning">Please enter your name</span><?php } ?></label><br />
                <input name="name" id="name" type="text" class="formbox" 
                	<?php if (isset($missing)) {
					echo 'value="' .htmlentities($_POST['name']).'"';
					} ?>
                />
            </p>
            <p>
                <label for="email">Email:</label><br />
                <input name="email" id="email" type="text" class="formbox"
                	<?php if (isset($missing)) {
					echo 'value="' .htmlentities($_POST['email']).'"';
					} ?>
                 />                
            </p>
            <p>
                <label for="comments">Comments:<?php 
				if (isset($missing) && in_array('comments', $missing)) { ?>
                    <span class="warning">Please enter your comments</span><?php } ?></label><br />
                <textarea name="comments" id="comments" cols="40" rows="8"><?php
                        if (isset($missing)) {
					echo htmlentities($_POST['comments']);
					} ?></textarea>
            </p>
                        <p>*All fields are required.</p>

            <p>
                <input name="send" id="send" type="submit" value="Send message" />
            </p>
        </form>
</div>
</div>
</body>
</html>

 

Any help would be greatly appreciated.

Ok, I figured out where my error was, I had to change the follow code:

 

   // Build the message
      $message = "Name: $name\n\n";
      $message .= "Email: $email\n\n";
      $message .= "Comments: $comments\n\n";

 

to

 

	// Build the message
	$message = "Name:" . $_POST['name'] . "\n\n";
	$message .= "Email:" . $_POST['email'] ."\n\n";
	$message .= "Comments:" . $_POST['comments'] . "\n\n";

 

I'm not really sure why this is, but it works now.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.