Strange PHP parse error when creating a contact form [duplicate] - php

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
For some reason I keep getting an error within my backend code for my contact form
Here is my contact form
<div id = "form">
<form action ="contact2.php" method="post">
Hi Rebekah My Name Is
<br>
<input type="text" name="name">
<br>
My Email Is
<br>
<input type="text" name="email">
<br>
My Message Is
<br>
<TEXTAREA NAME="message" ROWS=6 COLS=40>
</TEXTAREA>
<br>
<input type="submit" value="Submit">
<?php
include "contact2.php";
echo $result;
?>
</div>
Here is my backend PHP code
<?php
$field_name = $_POST['name'];
$email = $_POST['email'];
$field_message = $_POST['message'];
$mail_to = 'example#yahoo.com';
$subject = 'Message from a site visitor ' . $field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$email."\n";
$body_message .= 'Message: '.$field_message;
$headers = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
// Check if name has been entered
if (!$_POST['name']) {
$field_name = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$email = 'Please enter a valid email address';
}
//Check if message has been entered
if (!$_POST['message']) {
$field_message = 'Please enter your message';
}
// If there are no errors, send the email
if (!$field_name && !$email && !$field_message) {
if (mail ($_to, $subject, $body_message, $headers)) {
$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>';
}
?>
When I run the code there is said to be an error in my php code on line 43 which is the last line but I can't seem to see what the problem is as this is only the closing tag for the php code, when I take the closing php tag out I still get the same message.
Here is the message that is displayed.
Parse error: syntax error, unexpected end of file in D:\contact2.php on line 43
I have taken out where the file is found from the above for safety reasons.

You are missing the closing tag of your if statement.
Add } before your closing tag ?> on the last line.

Your if (!$field_name && !$email && !$field_message) { is not closed.

Your last IF statement is lacking a }
if (!$field_name && !$email && !$field_message) {
if (mail ($_to, $subject, $body_message, $headers)) {
$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>';
}
} // <-----

Related

Any idea why my mail function doesn't work? [duplicate]

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

Not Receiving Email From PHP Form [duplicate]

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

php echo htmlspecialchar post error

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.

Bootstrap Contact Form - How to send html email

I am using a bootstrap contact form. The form works fine but whatever I do I cannot get it to send html emails. I have added headers and css inlined to different parts of the script but it never sends the email with any html formatting. I am banging my head against the wall and would really appreciate any help. Where do I put the headers variable and where do I put the html?
Here is the php code.
<?php
if ($_POST["submit"]) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Mywebsite';
$to = 'me#email.com';
$subject = 'Contact Email ';
$headers = "";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "To: ". $to. "\r\n";
$headers .= "From: ". $from;
$body = " $headers, From: $name\n E-Mail: $email\n Message: $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 content 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) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! </div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
}
}
}
?>
And here is the HTML.
<div style="padding:10px; background:#000; font-size:46px; font-weight: 900; color:#fff; ">
My Website
</div>
<div style="padding:24px; font-size:17px; background:#DCDCDC ; ">
This is a message from My website.
<br>
<br>
<br>
Login in to your account
<br>
<br>
</div>
<br>
<br>
</div>
replace mail($to, $subject, $body, $from); with mail($to, $subject, $body, $headers);

Input form doesn't parse error and displays blank page on submit

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;}

Categories