I am a total newbie in terms of php and that's why I always used read made codes for my contact forms. The thing is I don't know how to prevent empty inputs from being sent to my email. I have a one-input form and I would like to validate it. I really searched about a solution, but unfortunately it is not working. I hope someone can help me. I'll put here my code:
<?php
if (isset($_POST["submit"])) {
$email = $_POST['email'];
$from = 'Felipe Felix';
$to = 'email#email.com';
$subject = 'My email listing';
$body = "E-mail: $email";
if (mail ($to, $subject, $body, $from)) {
header("Location: index.html/success.html");
} else {
header("Location: index/error.html");
}
}
?>
And my HTML:
<form id="emailListing" class="form-horizontal" action="index.php" method="POST" enctype="multipart/form-data" name="sentMessage" data-fv-framework="bootstrap" data-fv-icon-valid="glyphicon glyphicon-ok" data-fv-icon-invalid="glyphicon glyphicon-remove" data-fv-icon-validating="glyphicon glyphicon-refresh">
<div class="form-group">
<div class="col-xs-8 col-lg-9">
<input id="email" class="form-control" name="email" type="email" placeholder="メールアドレス" data-fv-emailaddress-message="This is not a valid email" />
</div>
<input id="submit" name="submit" type="submit" value="Submit" class="btn btn-default col-xs-4 col-lg-3">
</div>
</form>
I am also using this (something I saw in another question, but it's not working I guess):
<script>
$(document).ready(function() {
$('#emailListing').formValidation();
});
</script>
For the html page you need to have required='required' or only required attribute in in your input-(html5 feature) which must not empty while submitting form and use it like
<input id="email" class="form-control" name="email" type="email" placeholder="メールアドレス" data-fv-emailaddress-message="This is not a valid email" required />
If you want to jquery validator in front end you can have many plugins but you need to include those code in your html page using <script> tag,in the backend for validation you just need to
<?php
if (isset($_POST["submit"])) {
$email = $_POST['email'];
if(empty($email))
{
header("Location: index/error.html");
}
$from = 'Felipe Felix';
$to = 'email#email.com';
$subject = 'My email listing';
$body = "E-mail: $email";
if (mail ($to, $subject, $body, $from)) {
header("Location: index.html/success.html");
} else {
header("Location: index/error.html");
}
}
?>
Try this..
<?php
if (isset($_POST["submit"])) {
$email = $_POST['email'];
$from = 'Felipe Felix';
$to = 'email#email.com';
$subject = 'Contact';
$body = "E-mail: $email";
if ($_POST['email']){
if (mail ($to, $subject, $body, $from)) {
header("Location: index.html");
}
else {
header("Location: index.html");
}
}
else{
header("Location: index/error.html");
}
}
?>
You can double check the empty field both client side and server side:
client side using jquery before submission:
$(document).ready(function() {
$("#emailListing").on('submit', function(e) {
e.preventDefault(); //to prevent submit before check
if ($("#email").val()!=''){
$("#emailListing").submit();
}
else
{
alert('Please fill email field');
}
});
});
server side by php after from submission:
if (!empty($_POST[email])){
// Do something
}
The thing is, after trying some of the suggestions the other users gave to me, I end up with a form that sends the email address if you input it but sends it anyway even if you let it empty (the error page comes, bu sends the email anyway). So, I tried another way and it worked! My form now prevents the user from sending empty inputs and give them a feedback if the user sends a valid email address. I don't know if it's 100% correct in the syntax, but I'm not getting any errors. Thank you all for the support! I'll put here my code:
PHP
<?php
if (isset($_POST["submit"])) {
$email = $_POST['email'];
$from = 'Me';
$to = 'email#email.com';
$subject = 'My Subject';
$body = "E-mail Address: $email";
if ($_POST['email']){
if (mail ($to, $subject, $body, $from)) {
header("Location: http://website.com/success.html");
}
else{
header("Location: http://website.com/error.html");
}
}
else{
header("Location: http://website.com/error.html");
}
}
?>
HTML (I'm using bootstrap, that's why all those classes)
<form id="emailListing" class="form-horizontal" action="index.php" method="POST" enctype="multipart/form-data" name="sentMessage" onsubmit="return validateForm()">
<div class="form-group">
<div class="col-xs-8 col-lg-9">
<input id="email" class="form-control" name="email" type="email" placeholder="E-mail Address" />
</div>
<input id="submit" name="submit" type="submit" value="Subscribe" class="btn btn-default col-xs-4 col-lg-3">
</div>
</form>
JS
<script>
function validateForm() {
var x = document.forms["sentMessage"]["email"].value;
if (x == null || x == "") {
alert("メールアドレスを入力してください");
return false;
}
}
</script>
Related
Newb here, sorry in advance. I want to check for errors in a simple html form. I want the form to email me the customers information. I have my contact.php and errorcheck.php uploaded to my hosting provider. i actually made a working script contactform.php. (working) So I have a clue that my host can process the php code, but my errorcheck.php script is not working. Can anyone check my errorcheck.php script and see what may be causing the problem. all I want errorcheck.php to do so far, is display the error in the web address bar, just so I know that its working. Thanks in advance for helping a newb. I hope I explained my self and code well enough.
contactform.php (working script will connect and send me an email of form information)
errorcheck.php (will not connect or check the form for errors, I want to display the error in web address bar)
contact.php (this is the html form)
errorcheck.php code below
<?php
if (isset($_POST['submit'])) {
$first = $_POST['name'];
$mail = $_POST['mail'];
$subject = $_POST['subject'];
$message = $_POST['message'];
//check if inputs are empty
if (empty($first) || empty($mail) || empty($subject) || empty($message)) {
header("Location:contact.php?signup=empty");
exit();
} else {
if (!preg_match("/^[a-zA-Z]*$/", $first)) {
header("Location:contact.php?signup=char");
exit();
} else {
//check if email is valid
if (!fiter_var($mail, FILTER_VALIDATE_EMAIL)) {
header("Location:contact.php?signup=invalid-email");
exit();
} else {
header("Location:contact.php?signup=signup=success");
}
}
}
}
(html code will connect to contactform.php but not errorcheck.php)
contact.php form code is below
<form class="contact-form" action="contactform.php" method="post">
<label for="name">Name</label>
<input type="text" name="name" placeholder="Full name..">
<label for="mail">Email Address</label>
<input type="text" name="mail" placeholder="Your E-Mail..">
<label for="subject">Phone Number</label>
<input type="text" name="subject" placeholder="Phone Number..">
<label for="message">Message</label>
<textarea name="message" placeholder="Message.."></textarea>
<input type="submit" value="submit" name="submit"></input>
</form>
contactform.php code is below
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];
$mailTo = "sales#screenrunners.com";
$headers = "From: ".$mailFrom;
$txt = "You have received and e-mail from ".$name.".\n\n".$message;
mail($mailTo, $subject, $txt, $headers);
header("Location: contact.php?mailsend");
}
?>
Im trying to build a simple contact form for a Wordpress site. I want it to send an email and reload the contact page it was sent from with an error/success message.
I have installed WP mail SMTP and it sends the test email.
Now to the problems. I have a form in html that I want to use. This form has been put in a custom template.
The php is borrowed from guides I have found online and I have used several of them. None of them work for me. When I hit the submit button the page loads the base template (not the custom template that I'm using for the form page) and no mail is sent. I want it to load the same page but with an error/success div telling me what happened. If I run the php before pressing submit it gives me the error message.
Here is my php:
if(isset($_POST['submitButton'])){
//response generation function
$response = "";
//function to generate response
function my_contact_form_generate_response($type, $reason){
global $response;
if($type == "success") $response = "<div class='success'>{$reason}</div>";
else $response = "<div class='error'>{$reason}</div>";
}
//response messages
$not_human = "Human verification incorrect.";
$missing_content = "Please supply all information.";
$email_invalid = "Email Address Invalid.";
$message_unsent = "Message was not sent. Try Again.";
$message_sent = "Thanks! Your message has been sent.";
//user posted variables
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$phone = $_POST['phone'];
$human = 2;
//php mailer variables
$to = get_option('admin_email');
$subject = "Someone sent a message from ".get_bloginfo('name');
$headers = 'From: '. $email . "\r\n" .
'Reply-To: ' . $email . "\r\n";
if(!$human == 0){
if($human != 2) my_contact_form_generate_response("error", $not_human); //not human!
else {
//validate email
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
my_contact_form_generate_response("error", $email_invalid);
else //email is valid
{
//validate presence of name and message
if(empty($name) || empty($message)){
my_contact_form_generate_response("error", $missing_content);
}
else //ready to go!
{
$sent = mail($to, $subject, strip_tags($message),
$headers);
if($sent) my_contact_form_generate_response("success",
$message_sent); //message sent!
else my_contact_form_generate_response("error",
$message_unsent); //message wasn't sent
}
}
}
}
else if ($_POST['submitted'])
my_contact_form_generate_response("error", $missing_content);
}
?>
Here is my html for the "print error/success" and the form:
<?php echo $response; ?>
<form class="interest-form" action="<?php the_permalink(); ?>" method="POST">
<fieldset>
<legend>Anmäl Intresse:</legend>
<div class="row">
<div class="col-6">
<input type="text" class="form-control" placeholder="Namn" name="name" value="<?php echo esc_attr($_POST['name']); ?>">
</div>
<div class="col-6">
<input type="tel" class="form-control" placeholder="Telefonnummer" name="phone" value="<?php echo esc_attr($_POST['phone']); ?>">
</div>
</div>
<div class="row">
<div class="col">
<input type="email" class="form-control" placeholder="E-post" name="email" value="<?php echo esc_attr($_POST['email']); ?>">
</div>
<div class="col">
<input class="form-control" type="text" placeholder="<?php the_field('fb_8_1_plats'); ?> <?php the_field('fb_8_1_startdatum'); ?>" readonly name="plats_datum">
</div>
</div>
<div class="row">
<div class="col">
<div class="form-group">
<label for="meddelande1">Meddelande:</label>
<textarea class="form-control" rows="3" name="message" value="<?php echo esc_attr($_POST['message']); ?>" id="meddelande1"></textarea>
</div>
</div>
</div>
<input type="hidden" name="submitted" value="1">
<button type="submit" name="submitButton" class="btn btn-primary float-right">Skicka intresseanmälan</button>
</fieldset>
</form>
I really can't figure out what is wrong, I spent two full days just googling, trying and failing last weekend. After that I gave up and haven't looked at at for a week. Please help!
Edit: It doesn't load index.php, the path is still the same but it uses the template for index.php
This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
Hii guys I need some help.
When I try and test my bootstrap form, it displays a white screen. Here's my code.NB Am newbie to web developing, #ExcuseMyFrenchThough
Here is my index.html
<html>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3" id="offer">
<h2 id="form"> LET'S WORK ? </h2>
</div>
<div class="col-md-6 col-md-offset-3">
<form role="form" method="post" action="contact.php">
<div class="form-group">
<input type="text" class="form-control" placeholder="Enter Your Name">
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
<div class="form-group">
<input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter Your Email">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
<div class="form-group">
<textarea class="form-control" id="textarea1" rows="3" placeholder="Enter Your Message here"> </textarea>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
</div>
<div class="form-group">
<button type="submit" class="default-submit btn btn-large propClone bg-fast-pink btn-circle font-weight-300 text-white tz-text">SEND MESSAGE</button>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</form>
</div>
</div>
</div>
PHP CODE [CONTACT.PHP]
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
/*$human = intval($_POST['human']); */
$from = 'Geofrey Zellah';
$to = 'hotbonge#gmail.com';
$subject = 'Message from Geofrey Zellah ';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
/*
//Check if simple anti-bot test is correct
if ($human !== 5) {
$errHuman = 'Your anti-spam is incorrect';
} */
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage /*&& !$errHuman*/) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
}
}
?>
Screenshot of what I get after I click submit on my page, NB live not local
anyone ?
The submit variable is never set in your form. Note the button is now an input of the type submit with the name attribute of submit.
Also your other form variables were not set.
You were never echoing anything. So i put an echo in your last condition.
If you want the form to display after submitting the form, you need to rename your index.html to index.php and include contact.php at the top. See below.
If you just plainly check if a $_POST variable is true, PHP will throw E_NOTICE errors. So best wrap the variable into the isset() (as in is this variable set) function. See below.
I refactored to prevent E_NOTICE errors and commented the changes.
contact.php
<?php
if (isset($_POST["submit"])) {
$error = [];
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
/*$human = intval($_POST['human']); */
$from = 'Geofrey Zellah';
$to = 'hotbonge#gmail.com';
$subject = 'Message from Geofrey Zellah ';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
// Check if name has been entered
if (!isset($_POST['name']) || strlen($_POST['name']) === 0) {
$error['name'] = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!isset($_POST['email']) || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$error['email'] = 'Please enter a valid email address';
}
//Check if message has been entered
if (!isset($_POST['message']) || strlen($_POST['name']) === 0) {
$error['message'] = 'Please enter your message';
}
/*
//Check if simple anti-bot test is correct
if ($human !== 5) {
$errHuman = 'Your anti-spam is incorrect';
} */
// If there are no errors, send the email
if (empty($error)) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
}
}
?>
index.php <-- Note the change of file extension
<?php include 'contact.php';?>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3" id="offer">
<h2 id="form"> LET'S WORK ? </h2>
</div>
<div class="col-md-6 col-md-offset-3">
<form role="form" method="post">
<div class="form-group">
<input name="name" type="text" class="form-control" placeholder="Enter Your Name">
<?php if(isset($error['name'])) echo '<p class="text-danger">'.$error['name'].'</p>'; ?>
</div>
<div class="form-group">
<input name="email" type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter Your Email">
<?php if(isset($error['email'])) echo '<p class="text-danger">'.$error['email'].'</p>'; ?>
</div>
<div class="form-group">
<textarea name="message" class="form-control" id="textarea1" rows="3" placeholder="Enter Your Message here"> </textarea>
<?php if(isset($error['message'])) echo '<p class="text-danger">'.$error['message'].'</p>'; ?>
</div>
<div class="form-group">
<input name="submit" type="submit" class="default-submit btn btn-large propClone bg-fast-pink btn-circle font-weight-300 text-white tz-text">SEND MESSAGE</input>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php if(isset($result)) echo $result; ?>
</div>
</div>
</form>
</div>
</div>
</div>
UPDATE
If you do not want a reload of the page when submitting the form, you will need some jQuery ajax action and alter your HTML and PHP file.
First remove the first line of your index.php that we added before:
<?php include 'contact.php';?><!-- Remove this one -->
You do not want the file to be included, but rather send data to it.
Next edit the HTML file and include jQuery library underneath your HTML (common practice to do JS stuff below HTML). Then alter your PHP file accordingly.
So your new HTML:
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3" id="offer">
<h2 id="form"> LET'S WORK ? </h2>
</div>
<div class="col-md-6 col-md-offset-3">
<form role="form" name="contact" method="post">
<div class="form-group">
<input name="name" type="text" class="form-control" placeholder="Enter Your Name" value="test">
</div>
<div class="form-group">
<input name="email" type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter Your Email" value="giroteam#localhost.com">
</div>
<div class="form-group">
<textarea name="message" class="form-control" id="textarea1" rows="3" placeholder="Enter Your Message here">test </textarea>
</div>
<div class="form-group">
<input name="submit" type="submit" class="default-submit btn btn-large propClone bg-fast-pink btn-circle font-weight-300 text-white tz-text" value="SEND MESSAGE">
</div>
<div class="form-group" id="result">
</div>
</form>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){ // launch when DOM is fully loaded
$('form[name="contact"]').submit(function(event){ // fire when you hit submit
event.preventDefault(); // prevent default form submission since you want to send data via ajax
$('#result').html('');
$('.alert').remove();
var values = $(this).serialize();
// Post form data to your contact.php script and work with response
$.ajax({
url: "contact.php",
type: "POST",
data: values ,
success: function (response) {
if(response.success) {
$('#result').html('<div class="alert alert-success">'+response.success+'</div>');
}
if(response.error_form) {
$.each( response.error_form, function( key, value ) {
$('input[name="'+key+'"]').parent().append('<p class="help-block text-danger">'+value+'</p>');
});
}
if(response.error_mail) {
$('#result').html('<div class="alert alert-danger">'+response.error_mail+'</div>');
}
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
});
});
});
</script>
And finally the changed PHP:
<?php
ini_set('display_errors',0);
$result = [];
// Check if name has been entered
if (!isset($_POST['name']) || strlen($_POST['name']) === 0) {
$result['error_form']['name'] = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!isset($_POST['email']) || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$result['error_form']['email'] = 'Please enter a valid email address';
}
//Check if message has been entered
if (!isset($_POST['message']) || strlen($_POST['message']) === 0) {
$result['error_form']['message'] = 'Please enter your message';
}
/*
//Check if simple anti-bot test is correct
if ($human !== 5) {
$errHuman = 'Your anti-spam is incorrect';
} */
// If there are no errors, send the email
if (empty($result['error_form'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
/*$human = intval($_POST['human']); */
$from = 'Geofrey Zellah';
$to = 'hotbonge#gmail.com';
$subject = 'Message from Geofrey Zellah ';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if (mail ($to, $subject, $body, $from)) {
$result['success']='Thank You! I will be in touch';
} else {
$result['error_mail']='Sorry there was an error sending your message. Please try again later';
}
}
header('Content-type: application/json'); // tell browser what to expect
echo json_encode($result); // encode array to json object so javascript can work with it
I made such an elaborate example since many people decide to go ajax once they are successful in sending a regular form but notice that the page reloads ;)
I get what you want to achieve. You want to submit this form and if something is wrong, you want to show some error messages.
What has happened in your case is that, when you submit your form, it gets submitted to contact.php. Your browser will show what you return from the contact.php file. Since your contact.php do not include any HTML content and you don't write anything either, returned page doesn't include anything.
You have two options.
First way:
echo the error messages to the contact.php file. Add the following to the end of contact.php file.
echo $result;
This way, when someone submit your form. You will direct him to a new page which only have some line saying whether there was error or successful.
Second way
Move that logic to your index.html. You have to rename it to index.php. Then set the form to submit to the same page.
<form .... action="">
This will submit the form to the current page, which is index.php. Then, put your logic in contact.php at top of your index.php.
I have a simple contact form appearing on a modal in bootstrap. For some reason my form will not send. I've been looking at it for the last few hours and could use a fresh pair of eyes. It seems so simple but I'm at a total loss.
I've put a non-displayed textfield in the form as a spam filter. Since most bots would fill the textfield I want the code to send only if the textfield is blank (which it should be if the user is human since it's not displayed).
index.php
<div class = "modal fade" id = "contact" role = "dialog">
<div class = "modal-dialog">
<div class = "modal-content">
<div class = "modal-header">
<a id = "btn-close" data-dismiss = "modal">X</a>
</div>
<form>
<form method="post" action="index.php">
<fieldset id= "contact-fieldset">
<input name="name" type="text" id="name" class="text-input" placeholder="First and last name" required>
<input name="email" type="email" placeholder="Email address" required>
<textarea name="message" placeholder="Your Message" required></textarea>
<input name="bot-catch" type="text" id="bot-catch">
<input id="submit" class="submit-btn" name="submit" type="submit" value="Submit">
</fieldset>
</form>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Me';
$to = 'me#email.com';
$subject = 'Hello';
$human = $_POST['bot-catch'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit'] && $human == '') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $human != '') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
</form>
<div class = "modal-footer">
</div>
</div>
</div>
</div>
EDIT: So I have now gotten my message to send, but only if I put the PHP code into a separate file and link to it by . Only problem is- I do not want the user to be directed off of my page once they send a message. Is there anything I can do? I still can't figure out why it won't send on my index page.
EDIT: Well I got it working!! Thank you for all your help!
So I have a HTML form with some PHP but I'm not getting any email whatsoever after submitting it. I can't find the problem! can you help me out?
Tried changing the if(isset($_POST['submit'])) { to 'enviar' but still not working.
tried putting some echos to see where it's not working. the only statement that is stopping at the else statement is stripslashes.
The form snippet:
<div id="contact-wrapper">
<?php if(isset($hasError)) { //If errors are found ?>
<p class="error">Please check if you entered valid information.</p>
<?php } ?>
<?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
<p><strong>Email sent with success!</strong></p>
<p>Thank you for using our contact form <strong><?php echo $name;?></strong>, we will contact you soon.</p>
<?php } ?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="contactform">
<div>
<label for="name"><strong>Name:</strong></label>
<input type="text" size="50" name="contactname" id="contactname" value="" class="required" />
</div>
<div>
<label for="email"><strong>E-mail:</strong></label>
<input type="text" size="50" name="email" id="email" value="" class="required email" />
</div>
<div>
<label for="subject"><strong>Subject:</strong></label>
<input type="text" size="50" name="subject" id="subject" value="" class="required" />
</div>
<div>
<label for="message"><strong>Message:</strong></label>
<textarea rows="5" cols="50" name="message" id="message" class="required"></textarea>
</div>
<input type="submit" value="enviar" name="submit" id="submit" />
</form>
</div>
and the PHP:
<?php
//If the form is submitted
if(isset($_POST['submit'])) {
//Check to make sure that the name field is not empty
if(trim($_POST['contactname']) == '') {
$hasError = true;
} else {
$name = trim($_POST['contactname']);
}
//Check to make sure that the subject field is not empty
if(trim($_POST['subject']) == '') {
$hasError = true;
} else {
$subject = trim($_POST['subject']);
}
//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '') {
$hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+#[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
$hasError = true;
} else {
$email = trim($_POST['email']);
}
//Check to make sure comments were entered
if(trim($_POST['message']) == '') {
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['message']));
} else {
$comments = trim($_POST['message']);
}
}
//If there is no error, send the email
if(!isset($hasError)) {
$emailTo = 'myemail#email.com'; //Put your own email address here
$body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments:\n $comments";
$headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
?>
Before getting any email, you have to be sure the email is being sent.
$sent = mail($emailTo, $subject, $body, $headers);
var_dump($sent);
What does this code output when placed in the appropriate place?
Have you tried a simple php page to do a test Email (eg fixed content) and I assume the webserver has email configured and is prepared to relay for your address?
If this is WordPress contact form, for starters:
mail($emailTo, $subject, $body, $headers);
should be:
wp_mail($emailTo, $subject, $body, $headers);
See: wp_mail codex
Now you have to check your e-mail setup, as well as WordPress e-mail setup too.