PHP form submit success message inserts unwanted number after message - php

Created a php form with a success message that appears just below the Submit button upon successful mission. After adding some additional code in the php to create an email confirmation, I'm now noticing that a number "1" has been inserted in the line after my success message - see below:
Any ideas on how to make that number 1 go away? Code below:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$company = $_POST['company'];
$message = $_POST['message'];
$human = $_POST['human'];
$from = 'From: Page Name';
$to = 'email#mysite.com';
$subject = 'Service Inquiry';
$body = "From: $name\n E-Mail: $email\n Phone: $phone\n Company: $company\n
Message:\n $message";
// Confirmation email.
$conf_subject = 'Your recent inquiry';
$conf_sender = 'MY SITE <no-reply#mysite.com>';
$msg = $_POST['name'] . ",\n\nThank you for your recent inquiry. A member of
our team will respond to your message as soon as possible.\n\nThanks,\n\nMy
Company Team";
if ($_POST['submit']) {
if ($name != '' && $email != '' && $phone != '' && $message != '') {
if ($human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Thanks for your inquiry, we will get back to you as soon
as we can!</p>';
echo (mail ($email, $conf_subject, $msg, 'From: ' . $conf_sender
));
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
} else {
echo '<p>You need to fill in all required fields!</p>';
}
}
?>
Thanks everyone!

This line:
echo (mail ($email, $conf_subject, $msg, 'From: ' . $conf_sender));
You are echoing out the result of your call to the mail function. Since the mail was successfully handed over to the server it returns true. When you echo out a boolean true it gets converted to an integer which is 1. That's why you see that in your code.
Remove the echo to remove the 1 from being displayed in your output.

echo (mail ($email, $conf_subject, $msg, 'From: ' . $conf_sender ));
This code is causing the echo, its echoing 1 as the mail() function is returning true. I'm not sure why you're echoing the mail function here any way, just remove the echo and all is good.

That is because of this code:
echo (mail ($email, $conf_subject, $msg, 'From: ' . $conf_sender ));
You "echo" the result of the mail function. "1" is equal to "true" on mail sending success.

Related

PHP Contact Form Coming From Admin's Email

I made a PHP Contact Form using this tutorial and it works great, but I've encountered one potential security risk / inconvenience. Each email I receive comes from my admin login name.
I added $headers as this thread instructed, but to no avail.
My Current PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$from = $_POST['email'];
$to = 'myClientsEmail#gmail.com';
$subject = 'Estimate Contact Form';
$headers = "From: $email\r\n"; /* I added this */
$headers .= "Reply-To: $email\r\n"; /* and this */
$body = "From: $name\n Phone: $phone\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from, $headers)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
?>
What exactly am I missing? Any help is greatly appreciated. Thank you!
Your mail() function call has an extra parameter it looks like. The correct mail() call should be:
if (mail($to, $subject,$body,$headers)) {
....
}
So just remove the $from portion and it should be good.

PHP form throwing errors - Notice: Undefined index

I am trying to implement php for a simple form.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Hola';
$to = 'test#yahoo.com';
$subject = 'Hola';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
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>';
}
?>
<form class="form" method="post" action="say-hello.php">
<label>Name</label>
<input name="name" placeholder="Spongebob" required data-errormessage-value-missing="Whoa, you can't leave this blank!">
<label>Email</label>
<input name="email" type="email" placeholder="squarepants#krustykrab.com" required data-errormessage-value-missing="Whoa, you can't leave this blank!" data-errormessage-type-mismatch="Something isn't right...">
<label>Message</label>
<textarea name="message" placeholder="Well Hello!" required data-errormessage-value-missing="Whoa, you can't leave this blank!"></textarea>
<div class="bttnholder">
<input class="submit" name="submit" type="submit" value="Submit" placeholder="Send">
</div>
</form>
I can't figure out why part of my PHP is displayed as HTML and why I get the following errors on page:
Notice: Undefined index: name in C:\xampp\htdocs\sandbox\say-hello.php on line 35
Notice: Undefined index: email in C:\xampp\htdocs\sandbox\say-hello.php on line 36
Notice: Undefined index: message in C:\xampp\htdocs\sandbox\say-hello.php on line 37
Any help to get this code working?
The following are lines 35,36,37.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
You must first check variable to set. Try this:
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Hola';
$to = 'test#yahoo.com';
$subject = 'Hola';
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>';
}
}
The $_POST['submit'] is obsolete, as you should check for the correct form inputs.
I check for every form input to be set and not to be empty. If one of them is empty or not set -> error.
If all fields are given -> send the mail.
<?php
$from = 'From: Hola';
$to = 'test#yahoo.com';
$subject = 'Hola';
if(
!isset($_POST['name']) || empty($_POST['name']) ||
!isset($_POST['email']) || empty($_POST['email']) ||
!isset($_POST['message']) || empty($_POST['message'])
){
echo '<p>Please fill in all fields</p>';
}else{
$body = "From: " . $_POST['name'] . "\n E-Mail: " . $_POST['email'] . "\n Message:\n " . $_POST['message'] . "";
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>';
}
}
?>
To get rid of the Notices, you should change your php to:
<?php
if(isset($_POST['name'])) {
$name = $_POST['name'];
}
if(isset($_POST['email'])) {
$email = $_POST['email'];
}
if(isset($_POST['message'])) {
$message = $_POST['message'];
}
$from = 'From: Hola';
$to = 'test#yahoo.com';
$subject = 'Hola';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if (isset($_SERVER['CONTENT_LENGTH'])) {
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>';
}
Your mail function is wrong, there is no "from" argument in it, if you want a from argument your mail function should be like this:
First define a headers variable:
$headers = "From: $from";
And your mail function:
mail($to,$subject,$body,$headers)
You have to insert these lines inside the if statement.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Hola';
$to = 'test#yahoo.com';
$subject = 'Hola';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
When the html page loads, the name for example doesn't exists. It's been set after clicking on the submit button. So if you put it inside the if statement, the script will read the variables only after the submit button is posted.
Anyway, for your future consideration, these are not errors, are warnings, the script still works.

PHP form redirect (header) in external php file not working

I have a form on my homepage, which when submitted runs an external form.php file containing the code below. I am testing on MAMP and the header redirect doesn't seem to be working the url is just stuck on the form.php url? I have previously had an echo function which worked fine!? What am I doing wrong? Please help, many thanks in advance
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$company = $_POST['company'];
$robots = $_POST['robots'];
$from = 'From: Blah Register Form';
$to = 'sofi.smith#blah.com';
$subject = 'Blah Lead';
$body = "From: $name\n E-Mail: $email\n company: $company\n ";
if ($_POST['submit'] && $robots == '') {
if (mail ($to, $subject, $body, $from)) {
header("Location: http://google.com");
exit;
}
else {
echo '<p>Something went wrong, please try again</p>';
}
}
else if ($_POST['submit'] && $robots != '') {
echo 'Sorry, we don\'t like spammers here!';
}
?>
I can see a space before the first php tag, and you shouldn't close php tags either. That space means that content is sent before header.
However, your header statement can be failing due to several reasons (headers already sent, warning, etc). One quick dirty workaround is to use javascript for that matter:
if (mail ($to, $subject, $body, $from)) {
echo '<script>location.href="http://www.google.com";</script>';
exit;
}
You're condition will not always resolve to true in both scenarios. So unless you know for sure that the email is being sent, you don't know if any of the code is being called. This tightens it's up a bit:
if($_POST['submit']) {
if ($robots == '') {
if (mail($to, $subject, $body, $from)) {
header("Location: http://google.com");
} else {
echo 'Something went wrong, please try again';
}
} else {
echo 'Sorry, we don\'t like spammers here!';
}
} else {
echo "submit was not set";
}
Because right now you have:
if ($_POST['submit'] && $robots == '') {
// send the email or whatever
} else if ($_POST['submit'] && $robots != '') {
echo 'Sorry, we don\'t like spammers here!';
}
which means that if $_POST['submit'] is not set, neither condition would be true.
First of all write error_reporting(E_ALL); then you can see the exact error on page.
Also as i can see mail function it should be mail( $to , $subject , $message, $additional_headers )
I guess there is error in mail function thats why the header function is not working
Hope this works:
<?php
if($_POST) {
$name = mysql_real_escape_string(strip_tags($_POST['name']));
$email = mysql_real_escape_string(strip_tags($_POST['email']));
$company = mysql_real_escape_string(strip_tags($_POST['company']));
$robots = mysql_real_escape_string(strip_tags($_POST['robots']));
$from = 'From: Blah Register Form';
$to = 'sofi.smith#blah.com';
$subject = 'Blah Lead';
$body = "From: $name\n E-Mail: $email\n company: $company\n ";
if($robots == '') {
$mail = mail($to, $subject, $body, $from);
if($mail) {
header('Location: http://google.com');
exit();
} else {
echo '<p>Something went wrong, please try again</p>';
}
} else if($robost != '') {
echo 'Sorry, we don\'t like spammers here!';
}
}
?>

Open a "Thank You" -html page on submit (with php form mail)

I just installed a feedback form that uses php, but I'm very new to the language.
The form itself is working. My question is this:
At the moment the form echoes a "Thank You" string when it's submitted. Can I have it redirect the user to a html-page instead?
Here's is my php code:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Myname';
$to = 'info#mydomain.com';
$subject = 'mydomain.com feedback';
$human = $_POST['human'];
$answers = array('red','Red');
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit'] && in_array($human,$answers)) {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Thanks!</p>';
} else {
echo '<p>Something went wrong!</p>';
}
} else if ($_POST['submit'] && !in_array($human,$answers)) {
echo '<p>You ansered the captcha wrong!</p>';
}
?>
Instead of echoing '<p>Thanks!</p>';
Just change it to a header() function...
And direct to whatever URL you wanna go to...
Example....
if (mail ($to, $subject, $body, $from)) {
header('Location: http://www.example.com/');
} else {
Yes you can using header location.
Take a look at: http://php.net/manual/en/function.header.php

PHP Unknown sender

Hello I am fairly new to PHP and do not know a lot at the moment. I have modified a contact form an have come into some problems regarding the mail going straight to junk.
I assume this is for the reason that (unknown sender) keeps displaying in the email header. I would appreciate it if someone could help me correct this. The following is the code that I have implemented into the website:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Wirral PT Enquiry';
$to = 'joebloggs#hotmail.com';
$subject = 'Wirral PT Enquiry';
$human = $_POST['human'];
$headers = "enquiry#wirralpt.co.uk";
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if ($name != '' && $email != '') {
if ($human == '2') {
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 != '4') {
echo '<p> 1+1=2!! </p>';
}
} else {
echo '<p>You need to fill everything!!</p>';
}
}
?>
$from = 'From: Wirral PT Enquiry'; should contain the 'from' email address, not just the name:
$from = 'From: Wirral PT Enquiry <enquiry#wirralpt.co.uk>';
Try that?
try using
$headers = "Reply To :enquiry#wirralpt.co.uk";
Might work for you
also,
$headers = "From :enquiry#wirralpt.co.uk";
try both of these with you relevant email IDs
Change your headers to this:
$headers = 'From: enquiry#wirralpt.co.uk' . "\r\n" .
'Reply-To: enquiry#wirralpt.co.uk' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
and your mail should look this this:
mail ($to, $subject, $body, $headers)
This error can also be caused if while using SMTP settings for PhpMailer, $mail->IsSMTP(); is missed

Categories