Testing some HTML/PHP

Soldato
Joined
18 Aug 2011
Posts
2,853
Location
Norfolk
I was after a little help if possible please. I have created a php form for a webpage and tested it using XAMPP and it looks like all the variable etc return the correct data, but I don't have the facility to send the form by Email locally.

Is there any kind soul out there with the relevant hardware/software that could test this for me please?

Code:
<style> 
.error {
	color:#FF0000;
}
</style>
		
		<?php
			// define variables and set to empty values
			$nameErr = $emailErr = "";
			$name = $email = $comment = "";

			if ($_SERVER["REQUEST_METHOD"] == "POST") 
			{
   				if (empty($_POST["name"])) 
				{
     				$nameErr = "Name is required";
   				} 
				else 
				{
     				$name = test_input($_POST["name"]);
     				// check if name only contains letters and whitespace
     				if (!preg_match("/^[a-zA-Z ]*$/",$name)) 
					{
       					$nameErr = "Only letters and white space allowed"; 
     				}
   				}
   
   				if (empty($_POST["email"])) 
				{
     				$emailErr = "Email is required";
   				} 
				else 
				{
     				$email = test_input($_POST["email"]);
     				// check if e-mail address is well-formed
     				if (!filter_var($email, FILTER_VALIDATE_EMAIL)) 
					{
       					$emailErr = "Invalid email format"; 
     				}
   				}
     
   				if (empty($_POST["comment"])) 
				{
     				$comment = "";
   				} 
				else 
				{
     				$comment = test_input($_POST["comment"]);
   				}
			}

			function test_input($data) 
			{
   				$data = trim($data);
   				$data = stripslashes($data);
   				$data = htmlspecialchars($data);
   				return $data;
			}
		?>

	<p><span class="error">* required field.</span></p>
	
	<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>"> 
	
   	<label>Name:</label> <input type="text" name="name">
   	<span class="error">* <?php echo $nameErr;?></span>
   	<br><br>

   	<label>E-mail:</label> <input type="text" name="email">
   	<span class="error">* <?php echo $emailErr;?></span>
   	<br><br>

   	<label>Comment:</label> <textarea name="comment" rows="5" cols="40"></textarea>
   	<br><br>

	<input id="submit" input type="submit" name="submit" value="Submit">
	<br><br>
	</form>
	
	<?php
		//email the site owner
		$to      = '[email protected]';
		$subject = 'QUESTION TO ABC';
		$message = '$name has asked: "$comment."';
		$headers = 'From: $email' . "\r\n" .
		'Reply-To: [email][email protected][/email]' . "\r\n" .
		'X-Mailer: PHP/' . phpversion();

		mail($to, $subject, $message, $headers);
	
		//email the site visitor
		$userto      = '$email';
		$usersubject = 'Thank you for contacting ABC';
		$usermessage = 'Thank you, $name, for contacting ABC. Your query will be answered as soon as possible.';
		
		mail($userto, $usersubject, $usermessage, $headers);
	?> 
	
<?php
echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $comment;
echo "<br>";
echo $subject, $message;
echo "<br>";
echo $usersubject, $usermessage;
?>


Obviously the last bit was for testing purposes.


Do I need to create a $userheaders too?


Currently I think the code only checks for a valid e-mail format e.g:
[email protected]

Or does it check the email address exists e.g:
[email protected]

If not I guess an if{} would be useful?


Thanks a lot.
 
Associate
Joined
21 May 2013
Posts
1,991
filter_var($email, FILTER_VALIDATE_EMAIL)

The above checks that the given email ($email) conforms to the format of a valid email address (that is, whether the string has some valid characters, an '@' symbol, and an appropriate domain). Whether the email address is actually a real one used by somebody however is unknown. If the email address doesn't exist the target mailserver will usually send you an email telling you so, although not necessarily in real-time.
 
Soldato
OP
Joined
18 Aug 2011
Posts
2,853
Location
Norfolk
I'm guessing XAMPP is running under Windows? If so, Windows doesn't have a mail server. You need a local SMTP server or you need to setup sendmail.

There's a nice tutorial here.

Thanks.

I installed the test mail server tool and changed some of the XAMPP files.

It seems to work.

I seem to get (as the owner):
title: QUESTION TO ABC
content: $name has asked: "$comment."

I'd expect it to be something like "Jane has asked: Can you do XYZ please"

I seem to get (as the visitor):
title: Thank you for contacting ABC
content: Thank you, $name, for contacting ABC. Your query will be answered as soon as possible.


I would expect $name and $comment to be populated and return as per the entered String?

code below is:
PHP:
<style> 
.error {
	color:#FF0000;
}
</style>
		
		<?php
			// define variables and set to empty values
			$nameErr = $emailErr = "";
			$name = $email = $comment = "";

			if ($_SERVER["REQUEST_METHOD"] == "POST") 
			{
   				if (empty($_POST["name"])) 
				{
     				$nameErr = "Name is required";
   				} 
				else 
				{
     				$name = test_input($_POST["name"]);
     				// check if name only contains letters and whitespace
     				if (!preg_match("/^[a-zA-Z ]*$/",$name)) 
					{
       					$nameErr = "Only letters and white space allowed"; 
     				}
   				}
   
   				if (empty($_POST["email"])) 
				{
     				$emailErr = "Email is required";
   				} 
				else 
				{
     				$email = test_input($_POST["email"]);
     				// check if e-mail address is well-formed
     				if (!filter_var($email, FILTER_VALIDATE_EMAIL)) 
					{
       					$emailErr = "Invalid email format"; 
     				}
   				}
     
   				if (empty($_POST["comment"])) 
				{
     				$comment = "";
   				} 
				else 
				{
     				$comment = test_input($_POST["comment"]);
   				}
			}

			function test_input($data) 
			{
   				$data = trim($data);
   				$data = stripslashes($data);
   				$data = htmlspecialchars($data);
   				return $data;
			}
		?>

	<p><span class="error">* required field.</span></p>
	
	<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>"> 
	
   	<label>Name:</label> <input type="text" name="name">
   	<span class="error">* <?php echo $nameErr;?></span>
   	<br><br>

   	<label>E-mail:</label> <input type="text" name="email">
   	<span class="error">* <?php echo $emailErr;?></span>
   	<br><br>

   	<label>Comment:</label> <textarea name="comment" rows="5" cols="40"></textarea>
   	<br><br>

	<input id="submit" input type="submit" name="submit" value="Submit">
	<br><br>
	</form>
	
	<?php
		//email the site owner
		$to      = '[email protected]';
		$subject = 'QUESTION TO ABC';
		$message = '$name has asked: "$comment."';
		$headers = 'From: $email' . "\r\n" .
		'Reply-To: [email protected]' . "\r\n" .
		'X-Mailer: PHP/' . phpversion();

		mail($to, $subject, $message, $headers);
	
		//email the site visitor
		$userto      = '$email';
		$usersubject = 'Thank you for contacting ABC';
		$usermessage = 'Thank you, $name, for contacting ABC. Your query will be answered as soon as possible.';
		
		mail($userto, $usersubject, $usermessage, $headers);
	?>
 
Soldato
OP
Joined
18 Aug 2011
Posts
2,853
Location
Norfolk
On a side note the php code is in the .html.

I created a .htaccess file and it seems to prevent it from taking ages to load.

Is this the best move or would a separate php file be better?
 
Associate
Joined
4 Feb 2011
Posts
580
Location
Halifax
Strings in single quotes ' won't output PHP variables, as it's a string literal. Switch them to double quotes and it should work.

PHP:
$message = "$name has asked: \"$comment\".";

Or

PHP:
$message = $name . ' has asked: "' . $comment . '".';
 
Soldato
OP
Joined
18 Aug 2011
Posts
2,853
Location
Norfolk
Cheers again. All working.

Just a quick question. This bit:

PHP:
$headers = "From: $email" . "\r\n" .
		'Reply-To: [email protected]' . "\r\n" .
		'X-Mailer: PHP/' . phpversion();

Obviously tells the site owner that $email has emailed. I have done a similar thing for the other end:

PHP:
$userheaders = 'From: [email protected]' . "\r\n" .
		'Reply-To: [email protected]' . "\r\n" .
		'X-Mailer: PHP/' . phpversion();

The
PHP:
'Reply-To: [email protected]'
what is this?
 
Associate
Joined
21 May 2013
Posts
1,991
That's the email address that the recipient will send a message to if they hit 'reply' to the message in their mail client.

Example case, if a company is sending a promo email or something, they might send it from '[email protected]' - but people might reply asking questions or something like that, so they'd set the reply-to address as '[email protected]' so that if people reply it gets send through to the support department.

It's just a suggestion to the mail client. There is no guarantee that a person will always use that reply address when responding to the message. However, it's fairly standard functionality in most mail clients.
 
Back
Top Bottom