Can someone tell me why this is not displaying the form? If I go to the address that I have it hosted on, the page just displays "Your message has been sent. Thank you, ." It seems to just execute and display the last function.
<?php
function validateInput($data, $fieldName) {
global $errorCount;
if (empty($data)) {
echo "\"$fieldName\" is a required field.<br />\n";
++$errorCount;
$retval = "";
} else { // only clean up the input if it isn't empty
$retval = trim($data);
$retval = stripsplashes($retval);
}
return($retval);
}
function validateEmail($data, $fieldName) {
global $errorCount;
if (empty($data)) {
echo "\"$fieldName\" is a required field.<br />\n";
++$errorCount;
$retval = "";
} else { // only clean up the input if it isn't empty
$retval = trim($data);
$retval = stripsplashes($retval);
$pattern = "/^[\w-]+(\.[\w-]+)*#" . "[\w-]+(\.[\w-]+)*" . "(\.[[a-z]]{2,})$/i";
if (preg_match($pattern, $retval) == 0) {
echo "\"$fieldName\" is not a valid e-mail address.<br />\n";
++$errorCount;
}
}
return($retval);
}
function displayForm($Sender, $Email, $Subject, $Message) {
?>
<h2 style = "text-align:center">Contact Me</h2>
<form name="contact" action="ContactForm.php" method="post">
<p>Your name: <input type="text" name="Sender" value="<?php echo $Sender; ?>" /></p>
<p>Your E-mail: <input type="text" name="Email" value="<?php echo $Sender; ?>" /></p>
<p>Subject: <input type="text" name="Subject" value="<?php echo $Subject; ?>" /></p>
<p>Message:<br />
<textarea name="Message"><?php echo $Message; ?></textarea></p>
<p><input type="reset" value="Clear Form" /> <input type="submit" name="Submit" value="Send Form" /></p>
</form>
<?php
}
$ShowForm = TRUE;
$errorCount = 0;
$Sender = "";
$Email = "";
$Subject = "";
$Message = "";
if (isset($_POST['Submit'])) {
$Sender = validateInput($_POST['Sender'],"Your Name");
$Email = validateInput($_POST['Email'],"Your E-mail");
$Subject = validateInput($_POST['Subject'],"Subject");
$Message = validateInput($_POST['Message'],"Message");
if ($errorCount == 0)
$ShowForm = FALSE;
else
$ShowForm = TRUE;
}
if ($ShowForm == TRUE)
if ($errorCount>0) {// if there were errors
echo "<p>Please re-enter the form information below.</p>\n";
displayForm($Sender, $Email, $Subject, $Message);
} else {
$SenderAddress = "$Sender <$Email>";
$Headers = "From: $SenderAddress\nCC: $SenderAddress\n";
// Substitute your own e-mail address for recipient#example.com
$result = mail("recipient#example.com", $Subject, $Message, $Headers);
if ($result)
echo "<p>Your message has been sent. Thank you, " . $Sender . ".</p>\n";
else
echo "<p>There was an error sending your message, " . $Sender . ".</p>\n";
}
?>
You have:
$showForm = TRUE;
Which, when the form has not yet been submitted, will lead to the conditional statement always being true with $errorCount == 0. You don't call displayForm() in the else case of that conditional.
Hope this helps.
Ok, the error was because curly brace should've been after the if ($ShowForm == TRUE) instead of the if ($errorCount>0).
Related
I was doing an exercise from a book and copied exactly what was in there (to my knowledge) yet I still can't get it to process a valid E-Mail. I've tried different regular expressions but have had no luck. I'm pretty sure I missed something stupid (and sound pretty stupid asking for this but I am new to PHP). Can anyone take a look and see if you can see what I'm missing (or doing wrong)?
<!DOCTYPE html>
<html lang="en">
<head>
<title>Contact Me</title>
</head>
<header>
</header>
<body>
<?php
function validateInput($data, $fieldName) {
global $errorCount;
if (empty($data)) {
echo "\"$fieldName\" is a required field.<br />\n";
++$errorCount;
$retval = "";
} else {
$retval = trim($data);
$retval = stripslashes($retval);
}
return($retval);
}
function validateEmail($data, $fieldName) {
global $errorCount;
if(empty($data)) {
echo "\"$fieldName\" is a required field.<br />\n";
++$errorCount;
$retval = "";
} else {
$retval = trim($data);
$retval = stripslashes($retval);
$pattern = "/^[\w-]+(\.[\w-]+)*#" . "[\w-]+(\.[\w-]+)*" . "(\.[[a-z]]{2,})$/i";
if (preg_match($pattern, $retval)==0) {
echo "\"$fieldName\" is not a valid e-mail address.<br />\n";
++$errorCount;
}
}
return($retval);
}
function displayForm($Sender, $Email, $Subject, $Message) {
?>
<h2 style = "text-align:center">Contact Me</h2>
<form name=contact: action="ContactForm.php" method="post">
<p>Your Name: <input type="text" name="Sender" value="<?php echo $Sender; ?>" /></p>
<p>Your E-mail: <input type="text" name="Email" value="<?php echo $Email; ?>" /></p>
<p>Subject: <input type="text" name="Subject" value="<?php echo $Subject; ?>" /></p>
<p>Message: <br />
<textarea name="Message"><?php echo $Message; ?></textarea></p>
<p><input type="reset" value="Clear Form" />
<input type="submit" name="Submit" value="Send Form" /></p>
</form>
<?php
}
$showForm = TRUE;
$errorCount = 0;
$Sender = "";
$Email = "";
$Subject = "";
$Message = "";
if (isset($_POST['Submit'])) {
$Sender = validateInput($_POST['Sender'], "Your Name");
$Email = validateEmail($_POST['Email'], "Your Email");
$Subject = validateInput($_POST['Subject'], "Subject");
$Message = validateInput($_POST['Message'], "Message");
if($errorCount==0)
$showForm = FALSE;
else
$showForm = TRUE;
}
if ($showForm == TRUE) {
if ($errorCount>0)
echo "<p>Please re-enter the form information below.</p>\n";
displayForm($Sender, $Email, $Subject, $Message);
}
else {
$SenderAddress = "$Sender <$Email>";
$Headers = "From: $SenderAddress\n CC: $SenderAddress\n";
$result = mail("greg.englar#gmail.com", $Subject, $Message, $Headers);
if ($result)
echo "<p>Your message has been sent. Thank you, " . $Sender . ".</p>\n";
else
echo "<p>There was an error sending your message, " . $Sender . ".</p>\n";
}
?>
</body>
</html>
I think I have a good grasp as to what the code is doing and how it is working but I am clearly missing something. It doesn't accept any E-Mail as valid and always shows an error message. For reference the book I'm using is "PHP Programming with MySQL Second Edition" by Don Gosselin.
There's an error in the regular expression. Should be [a-z] instead of [[a-z]] at the end
I'm kinda new in PHP and I've created a form that should validate data and submit an error if any field is blank or incorrect. It doesn't tho. Even if email is wrong or any field is empty and the errors are shown it still sends an email. And the headers are not showing in the message. The only case when the errors are shown and the mail isn't send is the case when all fields are empty. Here's the code:
<?php
$NameErr = $EmailErr = $SubErr = $MessErr = "";
$Name = $Email = $Subject = $Message = "";
$header = "From: " . $Email . "Name: " . $Name . "\r\n";
$header .= "Content-Type: text/plain";
$To = "xxx#gmail.com";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["Name"])) {
$NameErr = "Name is required";
} else {
$Name = test_input($_POST["Name"]);
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"]);
if (!filter_var($Email, FILTER_VALIDATE_EMAIL)) {
$EmailErr = "Invalid email format";
}
}
if (empty($_POST["Subject"])) {
$SubErr = "Subject is required";
} else {
$Subject = test_input($_POST["Subject"]);
}
if (empty($_POST["Message"])) {
$MessErr = "Message is required";
} else {
$Message = test_input($_POST["Message"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<p><input class="w3-input w3-padding-16" type="text" placeholder="Name" name="Name"></p>
<span class="error"> <?php echo $NameErr; ?></span>
<p><input class="w3-input w3-padding-16" type="text" placeholder="Email" name="Email"></p>
<span class="error"> <?php echo $EmailErr; ?></span>
<p><input class="w3-input w3-padding-16" type="text" placeholder="Subject" name="Subject"></p>
<span class="error"> <?php echo $SubErr; ?></span>
<p><input class="w3-input w3-padding-16" type="text" placeholder="Message" name="Message"></p>
<span class="error"> <?php echo $MessErr; ?></span>
<p>
<button class="w3-btn w3-grey w3-padding-large w3-hover-green" type="submit" value="Submit" name="pressed">
<i class="fa fa-paper-plane"></i> SEND MESSAGE
</button>
</p>
</form>
<?php
if (isset($_POST["pressed"])) {
if (empty($NameErr && $SubErr && $MessErr && $EmailErr)) {
mail($To, $Subject, $Message, $header);
echo "Email sent.";
} else {
echo "Error.";
}
}
?>
Can you help me? Error validating is on and it doesn't show me any errors.
use isset function instead of empty() to check if the field is posted or not.
example:
if (!isset($_POST["Name"])) {
...
also there is no need to check the request method, $_POST will only catch post requests.
So I've re designed the code structure for you. Generally calling a class and a function will keep your files and your code cleaner.
So with this being said, let me show you some insight. This is where your form will be located for example: form.php
<?php
require ('mail.php');
$send = new Mail();
if (isset($_POST['sendIt']))
{
$send->sendMail($_POST['nameP'], $_POST['email'], $_POST['subject'], $_POST['message']); // Call the class and function
}
?>
<form id="contact" method="post">
<div class="container">
<input type="text" name="nameP" placeholder="Name *" /><br />
<input type="email" name="email" placeholder="Email *"/><br />
<input type="text" name="subject" placeholder="Subject *"><br />
<textarea name="message" id="" cols="30" rows="10"></textarea>
<input type="submit" name="sendIt" id="submit">
</div>
</form>
Then create yourself a mail.php file to store the class and the functions revolving around mailing in general:
<?php
class Mail
{
public function sendMail($name, $email, $subject, $message)
{
if (!empty($name))
{
if (!empty($email))
{
if (!empty($subject))
{
if (!empty($message))
{
$email_to = 'Your#emailAddress';
$header = 'From: ' . $name ."<noreply#youremail>". "\r\n" .
'Reply-To: ' . $email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, 'Enquiry Received', 'Name: ' . $name . "\r\n\r\n". 'Email Address: ' .$email."\r\n\r\n" . 'Message: ' .$message."\r\n\r\n". $header);
echo "SUCCESS MESSAGE";
} else {
echo "Please fill in your message";
}
} else {
echo "Please provide a subject.";
}
} else {
echo "Please provide your email address.";
}
} else {
echo "Please provide your name.";
}
}
}
?>
This will generally clear the form if there is an error by default however you can then simply add value="<?php echo $_POST['whateverThisFormIsFor'];?>
I hope this will help and give you some further insight.
The way you're constructing your empty check towards the bottom is incorrect:
if (empty($NameErr && $SubErr && $MessErr && $EmailErr)){
The only way that this will evaluate to false is if all of the error messages are set, and the above snippet will break before PHP 5.5 (as Felippe mentioned in the comments). What you want instead is the below; it returns true only if none of the error messages are set:
if (empty($NameErr)
&& empty($SubErr)
&& empty($MessErr)
&& empty($EmailErr)) {
Another way to do this would be to
extract the validation logic into methods for readability,
read off of an $errors array instead of $NameErr, $SubjectErr, etc.
keep POST logic together (instead of split between the beginning and end)
To those ends, I've rewritten your snippet below:
<?php
function validateName($input)
{
if (empty($input)) {
return 'Name is required';
}
if (preg_match("/^[a-zA-Z ]*$/", $input) != 1) {
return 'Name may only contain letters and spaces';
}
return null;
}
function validateEmail($input)
{
if (empty($input)) {
return 'Email is required';
}
if (filter_var($input, FILTER_VALIDATE_EMAIL)) {
return 'Email is in an invalid format';
}
return null;
}
function validateSubject($input)
{
return empty($input) ? 'Subject is required' : null;
}
function validateMessage($input)
{
return empty($input) ? 'Message is required' : null;
}
function test_input($data)
{
return htmlspecialchars(stripslashes(trim($data)));
}
$errors = [];
$notification = '';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = test_input($_POST['name'] ?: '');
$email = test_input($_POST['email'] ?: '');
$subject = test_input($_POST['subject'] ?: '');
$message = test_input($_POST['message'] ?: '');
if (($error = validateName($name)) !== null) {
$errors['name'] = $error;
}
if (($error = validateEmail($email)) !== null) {
$errors['email'] = $error;
}
if (($error = validateSubject($subject)) !== null) {
$errors['subject'] = $error;
}
if (($error = validateMessage($message)) !== null) {
$errors['message'] = $error;
}
if (empty($errors)) {
$headers = [
"From: $name <$email>",
"Content-Type: text/plain",
];
$to = "xxx#gmail.com";
mail($to, $subject, $message, implode("\r\n", $headers));
$notification = 'The email was sent!';
} else {
$notification = 'The email could not be sent; please check below for errors.';
}
}
?>
<form method="post" action="<?= htmlspecialchars($_SERVER['PHP_SELF']) ?>">
<?php if (!empty($notification)): ?><p><?= $notification ?></p><?php endif; ?>
<p><input class="w3-input w3-padding-16" type="text" placeholder="Name" name="name" required></p>
<?php if (isset($errors['name'])): ?><span class="error"> <?= $errors['name'] ?></span><?php endif; ?>
<p><input class="w3-input w3-padding-16" type="email" placeholder="Email" name="email" required></p>
<?php if (isset($errors['email'])): ?><span class="error"> <?= $errors['email'] ?></span><?php endif; ?>
<p><input class="w3-input w3-padding-16" type="text" placeholder="Subject" name="subject" required></p>
<?php if (isset($errors['subject'])): ?><span class="error"> <?= $errors['subject'] ?></span><?php endif; ?>
<p><input class="w3-input w3-padding-16" type="text" placeholder="Message" name="message" required></p>
<?php if (isset($errors['message'])): ?><span class="error"> <?= $errors['message'] ?></span><?php endif; ?>
<p>
<button class="w3-btn w3-grey w3-padding-large w3-hover-green" type="submit">
<i class="fa fa-paper-plane"></i> SEND MESSAGE
</button>
</p>
</form>
I require some assistance that I hope that one of you can answer for me. I have an assignment for my PHP course. For this assignment we were tasked with making a form with 4 input fields (including a reset and submit button). The fields are supposed to be labeled as name, address, email, and phone number. Now when I write the code into Dreamweaver I get no syntax errors but whenever I execute the script using Wamp, the form doesn't show. Any help would be appreciated as this is something that has to be done for our midterm.
<?php
function validateInput($data, $fieldName) {
global $errorCount;
if (empty($data)) {
echo "\"$fieldName\" is a required field. <br />\n";
++$errorCount;
$retval = "";
} else { //Only clean up the input if it isn't // empty
$retval = trim($data);
$retval = stripslashes($retval);
}
return($retval);
}
function validateEmail ($data, $fieldName) {
global $errorCount;
if (empty($data)) {
echo "\"$fieldName\" is a required field.<br />\n";
++$errorCount;
$retval = "";
} else { // Only Clean up the input if it isn't // empty
$retval = trim($data);
$retval = stripslashes($retval);
$pattern = "/^[\w-]+(\.[\w-]+)*#" .
"[\w-]+(\.[\w-]+)*" .
"(\. [[a-z]]{2,})$/i";
if (preg_match($pattern, $retval)==0) {
echo "\"$fieldName\" is not a valid e-mail address.<br />\n";
++$errorCount;
}
}
return($retval);
}
function displayForm($Sender, $Email, $Address, $Phone) {
?>
<h2 style= "text-align:center">Contact Us</h2>
<form name="contact" action="contact_us.php" method="post">
<p>Your Name: <input type="text" name="Sender" value="<?php
echo $Sender; ?>" /> </p>
<p>Your E-mail: <input type="text" name="Email" value="<?php echo $Email; ?>" /></p>
<p>Address: <input type="text" name="Address" value="<?php echo $Address; ?>" /></p>
<p>Phone #: <input type="number" name="Phone" value="<?php echo $Phone; ?>"<br />
</p>
<p><input type="reset" value="Clear Form" /> <input type="submit" name="Submit" value="Send Form" /></p>
</form>
<?php
}
$ShowForm = TRUE;
$errorCount = 0;
$Sender = "";
$Email = "";
$Address = "";
$Phone = "";
if (isset($_POST['Submit'])) {
$Sender =
validateInput($_POST['Sender'], "Your Name");
$Email =
validateEmail($_POST['Email'], "Your E-mail");
$Subject =
validateInput($_POST['Address'], "Your Address");
$Message =
validateInput($_POST['Phone'],"Your number");
if ($errorCount==0)
$ShowForm = FALSE;
else
$ShowForm = TRUE;
}
if ($ShowForm == TRUE) {
if ($errorCount>0) // if there were errors echo "<p>Please re-enter the form information below.</p>\n";
displayForm($Sender, $Email, $Address, $Phone);
}
else {
$SenderAddress= "$Sender <$Email>";
$Headers= "From: $SenderAddress\nCC:
$SenderAddress\n";
// Substitute your own email address for // recipient#example.com
$result = mail ("recipient#example.com",
$Subject, $Message, $Headers);
if ($result)
echo "<p>Your message has been sent. Thank you, " . $Sender . ".</p>\n";
else
echo "<p>There was an error sending your message, " .
$Sender . ".</p>\n";
} ?>
You see nothing becouse you din`t call the function displayForm() you just create it.
so put this displayForm(); before the end of your php tag and see what will happen.
if ($ShowForm == TRUE) {
if ($errorCount>0) // if there were errors echo "<p>Please re-enter the form information below.</p>\n";
displayForm($Sender, $Email, $Address, $Phone);
}
It looks like your form is set to only show when $ShowForm == TRUE && $errorCount > 0. Since your defaults are $ShowForm = TRUE and $errorCount = 0 your form will never show.
This is why using curly braces on your ifs, while making the file larger, help greatly when troubleshooting.
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
I keep getting this syntax error for the line where the closing tag is and I can not for the life of me figure out what I did wrong. I thought I followed the instructions for my textbook for this assignment but I obviously didn't if I am getting an error. I must be blind because I can't spot the error. Any help would be greatly appreciated.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Contact Me</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php
function validateInput($data, $fieldName) {
global $errorCount;
if (empty($data)) {
echo "\"$fieldName\" is a required field. <br />\n";
++$errorCount;
$retval = "";
} else { //Only clean up the input if it isn't // empty
$retval = trim($data);
$retval = stripslashes($retval);
}
return($retval);
}
function validateEmail ($data, $fieldName) {
global $errorCount;
if (empty($data)) {
echo "\"$fieldName\" is a required field.<br />\n";
++$errorCount;
$retval = "";
} else { // Only Clean up the input if it isn't // empty
$retval = trim($data);
$retval = stripslashes($retval);
$pattern = "/^[\w-]+(\.[\w-]+)*#" .
"[\w-]+(\.[\w-]+)*" .
"(\. [[a-z]]{2,})$/i";
if (preg_match($pattern, $retval)==0) {
echo "\"$fieldName\" is not a valid e-mail address.<br />\n";
++$errorCount;
}
}
return($retval);
}
function displayForm($Sender, $Email, $Subject, $Message) {
?>
<h2 style= "text-align:center">Contact Me</h2>
<form name="contact" action="ContactForm.php" method="post">
<p>Your Name: </p>
<p>Your E-mail:
<input type="text" name="Sender" value="<?php
echo $Sender; ?>" />
<input type="text" name="Email" value="<?php echo $Email; ?>" /></p>
<p>Subect: <input type="text" name="Subject" value="<?php echo $Subject; ?>" /></p>
<p>Message:<br />
</p>
<p><input type="reset" value="Clear Form" /> <input type="submit" name="Submit" value="Send Form" /></p>
</form>
<?php
}
$ShowForm = TRUE;
$errorCount = 0;
$Sender = "";
$Email = "";
$Subject = "";
$Message = "";
if (isset($_POST['Submit'])) {
$Sender =
validateInput($_POST['Sender'], "Your Name");
$Email =
validateEmail($_POST['Email'], "Your E-mail");
$Subject =
validateInput($_POST['Subject'], "Subject");
$Message =
validateInput($_POST['Message'],"Message");
if ($errorCount==0)
$ShowForm = FALSE;
else
$ShowForm = TRUE;
}
if ($ShowForm == TRUE) {
if ($errorCount>0) // if there were errors echo "<p>Please re-enter the form information below.</p>\n";
displayForm($Sender, $Email, $Subject, $Message);
}
else {
$SenderAddress= "$Sender <$Email>";
$Headers= "From: $SenderAddress\nCC:
$SenderAddress\n";
// Substitute your own email address for // recipient#example.com
$result = mail ("recipient#example.com",
$Subject, $Message, $Headers);
if ($result)
echo "<p>Your message has been sent. Thank you, " . $Sender . ".</p>\n";
else
echo "<p>There was an error sending your message, " .
$Sender . ".</p>\n";
</body>
</html>
You are not closing the last else block
else {
$SenderAddress= "$Sender <$Email>";
$Headers= "From: $SenderAddress\nCC:
$SenderAddress\n";
// Substitute your own email address for // recipient#example.com
$result = mail ("recipient#example.com",
$Subject, $Message, $Headers);
if ($result)
echo "<p>Your message has been sent. Thank you, " . $Sender . ".</p>\n";
else
echo "<p>There was an error sending your message, " .
$Sender . ".</p>\n";
} ?>
There's a couple of things that I need some help on:
I have a function called isValid that is not checking if the emails entered in my form are valid?
How can i have my error messages display inside the form's text field?
Any help is greatly appreciated!
Below is my code:
<?php
//Set Variables to Empty String
$Email = " ";
$Subject = " ";
$Name = " ";
$Message = " ";
$error = " ";
if(isset($_POST['submit']) )
{
if (empty($_POST["Email"]))
{
$error = "** Enter a valid email";
}
else
{
$Email = isValid($_POST["Email"]);
}
if (empty($_POST["Subject"]))
{
$error = "** Enter a subject";
}
else
{
$Subject= test_input($_POST["Subject"]);
}
if (empty($_POST["Name"]))
{
$error = "** Enter your name";
}
else
{
$Name= test_input($_POST["Name"]);
}
if (empty($_POST["Message"]))
{
$error = "** Enter your message";
}
else
{
$Message= test_input($_POST["Message"]);
}
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
//isValid checks if email address is a valid one
function isValid($edata)
{
return(preg_match("/^[-_.[:alnum:]]+#((([[:alnum:]]|[[:alnum:]][[:alnum:]-]* [[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][ 0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i", $edata));
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<p><label>From (Email):</label></p>
<input type="text" size="35" name="Email">
<span class="error"><?php echo $error;?></span>
<br><br>
<p><label>Subject:</label></p>
<input type="text" size="35" name="Subject">
<span class="error"><?php echo $error;?></span>
<br><br>
<p><label>Name:</label></p>
<input type="text" size="35" name="Name">
<span class="error"><?php echo $error;?></span>
<br><br>
<p><label>Message:</label></p>
<textarea type="text" cols="38" rows="6" name="Message"></textarea>
<span class="error"><?php echo $error;?></span>
<br><br>
<input type="submit" name="submit" value="submit">
<input type="reset" value="Reset">
</form>
<?php
if(empty($error))
{
// the email will be sent here
$to = "#gmail.com";
// the email subject
$subject = 'Message from XXXX website from: ' . $Name;
// the mail message
$msg .= "\r\nEmail: $Email";
$msg .= "\r\n\nSubject: $Subject";
$msg .= "\r\n\nName: $Name";
$msg .= "\r\n\nMessage: $Message";
mail($to, $subject, $msg, "From: $Email\r\nReply-To: $Email\r\nReturn-Path: $Email\r\n");
}
?>
<p>Thank you <b><?=$Name;?></b> for your message. Expect a response in 1 - 3 business days</p>
validate functions
<?php
$email_a = 'joe#example.com';
$email_b = 'bogus';
if (filter_var($email_a, FILTER_VALIDATE_EMAIL)) {
echo "This ($email_a) email address is considered valid.";
}
if (filter_var($email_b, FILTER_VALIDATE_EMAIL)) {
echo "This ($email_b) email address is considered valid.";
}
?>
for second question, change your code like this:
if (empty($_POST["Email"]))
{
$error['Email'] = "** Enter a valid email";
}
.
.
.
if (empty($_POST["Name"]))
{
$error['Name'] = "** Enter your name";
}
.
.
.
<input type="text" size="35" name="Email">
<span class="error"><?php echo $error['Email'];?></span>
<input type="text" size="35" name="Name">
<span class="error"><?php echo $error['Name'];?></span>