I cannot figure out how to POST select options. The form posts empty values for the contact (option) field. I know the selected_val is incorrect but not sure how to correct.
The form posts fine, but contact returns an empty value. Can anyone help out with how to correct the PHP.
Here's the PHP
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$city = $_POST['city'];
$selected_val = $_POST['contact']; "You have selected :" .$selected_val;
$message = $_POST['message'];
$from = 'From your website';
$to = 'somedomain.com';
$subject = 'Visitor Inquiry';
$body = "From: $name\n E-Mail: $email\n Phone: $phone\nCity: $city\nContact: $contact\nMessage: $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 business has been entered
if (!$_POST['company']) {
$errBusiness = 'Please enter your business or organization name';
}
// Check if phone has been entered
if (!$_POST['phone']) {
$errPhone = 'Please enter your phone number';
}
// Check if phone has been entered
if (!$_POST['phone']) {
$errPhone = 'Please enter the name of your city';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You!
We will be in touch soon.</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
}
}
}
?>
HTML
<form class="form-horizontal form-elements" role="form" method="post" action="services-form.php">
<div class="col-md-8 col-md-offset-2">
<label for="contact">Contact preference</label>
<select class="form-control" id="contact" name="contact">
<option>Email</option>
<option>Phone</option>
</select>
</div>
</form>
value attribute missing It should be like this
<select class="form-control" id="contact" name="contact">
<option>select</option>
<option value="email">Email</option>
<option value="phone">Phone</option>
</select>
You need value on option tags.
<option value="email">Email</option>
<option value="phone">Phone</option>
And I don't see any input name submit in your HTML
if( isset($_POST["submit"]) ) {
If so, it's not even go in to your code.
Also this line of your PHP code is doing nothing.
"You have selected :" .$selected_val;
should be
print "You have selected :" .$selected_val;
or
echo "You have selected :" .$selected_val;
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
HTML Code:
<form method="post" action="form.php">
<div class="col-sm-6 form-group">
<input class="form-control" id="name" name="name" type="text" placeholder="Name">
</div>
<div class="col-sm-6 form-group">
<input class="form-control" id="email" name="email" type="email" placeholder="Email">
</div>
<div class="col-xs-12">
<textarea class="form-control" id="message" name="message" type="message" rows="5" placeholder="Message"></textarea>
</div>
<button type="button" id="submit">Submit</button>
<?php echo $result; ?>
</form>
PHP Code:
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Demo Form';
$to = 'example#gmail.com';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
if (!$errName && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You</div>';
} else {
$result='<div class="alert alert-danger">Please try again later</div>';
}
}
}
?>
So this is the code and I don't know why I still don't get mail when the form is submitted. I tried everything I know but is it still the same. Moreover, I am using a web hosting that supports send emails and PHP and more. And it still doesn't work.
There are multiple logical and syntactical issues there.
You need to validate $_POST before you assign it to any variable.
You are using $subject but you do not have any value in that variable.
You are mixing header values to the mail body
I tried to fix issues try this.
<?php
if (isset($_POST["submit"])) {
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Demo Form';
$to = 'example#gmail.com';
$body = $message;
$subject="Demo message";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n".'X-Mailer: PHP/' . phpversion();
if (!$errName && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $headers)) {
$result='<div class="alert alert-success">Thank You</div>';
} else {
$result='<div class="alert alert-danger">Please try again later</div>';
}
}
}
?>
For further details of refer this http://php.net/manual/en/function.mail.php
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I am not receiving emails from my php form / html. I am new to making these forms, and I am wondering where I went wrong. The form sets up fine on a live site, and the submit button takes me back to the home page, which is what I want. Would anyone be able to help me figure out why I am not receiving the emails?
Here is the html :
`
<section class="contact-container">
<div class="container mtb">
<div class="row">
<div class="col-md-8 wow fadeInLeft">
<h4>Get in touch</h4>
<hr>
<p>Leave a comment, review, or general musings.</p><br />
<form id="contact_form" action="websitehere" method="post">
<label>Name: <input class="textfield" name="name" type="text" value="" /></label>
<label>Email: <input class="textfield" name="email" type="text" value="" /></label>
<label>Message: <textarea class="textarea" cols="45" name="message" rows="5"></textarea></label>
<input class="button" name="submit" type="submit" value="Submit" />
</form>
</div>
And here is the PHP:
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$to = 'emailaddress#email.com';
$subject = 'Message for the author ';
$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>';
}
}
}
?>
You forgot human in your html code. Also, make sure you are not sending this email to a Yahoo email because they have problems with php mails since December 4th. If that doesn't fix your problem, this might help you: http://email.about.com/od/emailprogrammingtips/qt/How_to_Send_Email_from_a_PHP_Script.htm
Let me start by saying this is the first time I have touched php since college, and I am very limited in my knowledge. I followed a tutorial in order to implement a contact form on a bootstrap website for a client. Everything works except for the "message" field due to it being a textarea rather than an input. For testing I swapped the textarea with an input, and it worked fine. Is there another method of capturing the user entered text to post? It seems like something simple, but I have been struggling with it for hours, and can't seem to find a solution. I am using the following code for the message html:
<div class="form-group">
<label for="message" class="col-sm-2 control-label">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="Enter Your Message" ><?php echo htmlspecialchars($_POST['message']);?></textarea>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
</div>
</div>
This is the php I am using, and it results in displaying the error message 'Please enter your message'
if ($_POST["submit"]) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'bot#dtosolutions.com';
$to = 'eric#ericbelldesigns.com';
$subject = 'Message from Contact Form';
$body ="From: $name\n E-mail: $email\n Message:\n $message";
//Check if name is entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
//Check if email is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email';
}
//Check if a message is entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
//Check if anti-bot test is correct
if ($human !== 7) {
$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.</div>';
}
}
}
It showing the error message Please Enter you Message because your message is not posting to your php script. According to your code you're checking the message in this line
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
but there are no input or textarea field named message. For posting your message to your php script you should change your textarea name Enter Your Message to message and then it will work perfectly.
I'm not sure why you want to print the message with <?php echo htmlspecialchars($_POST['message']);?>, but I see some possible problems here.
There is not an input with name message. The text area's name is
Enter Your Message. And name with spaces will be converted to
underscores in $_POST. That means you need to refer to
$_POST["Enter_Your_Message"].
I suggest you check if an input is filled before assign its value to a variable.
I am using the code below to send an email a short description.
I am getting the error below and I don't understand why,
<?php echo "<p class='text-danger'>
$errName
</p>
";?>
why this is giving error ?
here is the full code
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Demo Contact Form';
$to = 'example#domain.com';
$subject = 'Message from Contact Demo ';
$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>';
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Bootstrap contact form with PHP example by BootstrapBay.com.">
<meta name="author" content="BootstrapBay.com">
<title>Bootstrap Contact Form With PHP Example</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h1 class="page-header text-center">Contact Form Example</h1>
<form class="form-horizontal" role="form" method="post" action="index.php">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<?php if (isset($_POST['name'])){ echo htmlspecialchars($_POST['name']); } ?>">
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="example#domain.com" value="<?php if (isset($_POST['email'])){ echo htmlspecialchars($_POST['email']); } ?>">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
</div>
<div class="form-group">
<label for="message" class="col-sm-2 control-label">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message"><?php if (isset($_POST['message'])){ echo htmlspecialchars($_POST['message']); } ?></textarea>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
</div>
</div>
<div class="form-group">
<label for="human" class="col-sm-2 control-label">2 + 3 = ?</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
<?php echo "<p class='text-danger'>$errHuman</p>";?>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input id="submit" type="submit" name="submit" value="submit" class="btn btn-primary">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
i am really looking for some one to help it would be really helpful if some-one help me
thanks
You need to define $errName, like $errName="", before submit, try this:
<?php
$errName ="";
$errMessage ="";
$errHuman ="";
$result="";
$errEmail ="";
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Demo Contact Form';
$to = 'example#domain.com';
$subject = 'Message from Contact Demo ';
$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>';
}
}
}
?>
You need to use here on load before submit.On load first time you should declare the value as $errName = '';
<?php
$errName = '';
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Demo Contact Form';
$to = 'example#domain.com';
$subject = 'Message from Contact Demo ';
$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>';
}
}
}
?>
Replace the respective piece of code with this. (For similar cases adjust it)
if (isset($errName)) {
echo '<p class="text-danger">'.$errName.'</p>';
}
Note that in case of using echo <p class='text-danger'>$errName</p> with single quotes you will need to concatenate the $errName with the rest of the string because otherwise it will be printed like "$errName".
EDIT: Changed PHP to recommended code, still can't get success/error to display on page.
I'm trying to get this contact page to work, but it seems to be breaking when I click the submit button. As soon as I click, it redirects me to /contact_form.php and shows a blank page. If all forms have been correctly filled in, the message does get sent. If any of them are wrong or not filled in, the same blank page is shown.
HTML:
<form method="post" action="contact_form.php">
<div class="input-group-lg">
<label for="name">Your Name<i>*</i></label>
<input name="name" type="text" class="form-control" placeholder="What should I call you?">
</div>
<div class="input-group-lg">
<label for="email">Email Address<i>*</i></label>
<input name="email" type="email" class="form-control" placeholder="Email Address">
</div>
<div class="input-group-lg">
<label for="phone">Phone Number</label>
<input name="phone" type="text" class="form-control" placeholder="Phone Number">
</div>
<div class="input-group-lg">
<label for="message">Message<i>*</i></label>
<textarea name="message" class="form-control" placeholder="What's on your mind?"></textarea>
</div>
<div class="input-group-lg">
<label for="human">What is 2 + 2? (Anti-spam)<i>*</i></label>
<input name="human" type="text" class="form-control" placeholder="2 + 2 = ?">
</div>
<div class="text-center">
<?php echo $result; ?>
</div>
<div class="text-center">
<input name="submit" type="submit" value="Send Message" class="btn btn-custom"></input>
</div>
</form>
PHP:
<?php
if ($_POST["submit"]) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'From: example';
$to = 'example#gmail.com';
$subject = 'Message from X';
$body ="From: $name\n E-Mail: $email\n Phone: $phone\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 !== 4) {
$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)) {
print '<div class="alert alert-success">Thank You! I will be in touch</div>';
exit();
}
else {
print '<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
exit();
}
}
}
?>
Try this. I havnt tested it, but this should work like a charm. If you still get errors please post them.
if (isset ($_POST['name'])){ //RUN ONLY IF name IS SET
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$human = intval($_POST['human']);
// Error handling for missing data
if ((!$name) || (!$email) || (!$phone) || (!$message) || ($human !== 4)){
$result = '<div class="alert alert-danger"><i class="glyphicon glyphicon-warning-sign"></i> <strong>ERROR:</strong> You did not submit the following required information:</div>';
if(!$name){
$result .= '<div class="alert alert-danger"><strong>Please enter your name</strong></div>';
}else if(!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
$result .= '<div class="alert alert-danger"><strong>Please enter a valid email address</strong></div>';
}else if(!$phone){
$result .= '<div class="alert alert-danger"><strong>Please enter a phone number</strong></div>';
}else if(!$message){
$result .= '<div class="alert alert-danger"><strong>Please enter your message</strong></div>';
}else if($human !== 4){
$result .= '<div class="alert alert-danger"><strong>Please correct your addition</strong></div>';
}
} else { // Error handling is ended, process form and send email
$from = 'From: example';
$to = 'example#gmail.com';
$subject = 'Message from X';
$body ="From: $name\n E-Mail: $email\n Phone: $phone\n Message:\n $message";
mail($to, $subject, $body, $from);
print '<div class="alert alert-success">Thank You! I will be in touch</div>';
exit();
} // Close else after duplication checks
} else { // if the form is not posted with variables, place default empty variables so no warnings or errors show
$name = "";
$email = "";
$phone = "";
$message = "";
$human = "";
$result = "";
print '<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
exit();
}
You have no print or echo statement in your PHP file ...
add echo $result to the end of your PHP file to see the results
Change your success code this. This will print your success or error msg on page reload.
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
if (mail ($to, $subject, $body, $from)) {
print '<div class="alert alert-success">Thank You! I will be in touch</div>';
exit();
} else {
print '<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
exit();
}
}
Determine if a variable is set:
if (!isset($errName) && !isset($errEmail) && !isset($errMessage) && !isset($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>';
}
}
if(isset($result)){
echo $result;}