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?
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.
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.