This question already has answers here:
PHP Page shows up raw code
(3 answers)
Closed 8 years ago.
I have the following PHP script that runs when a send button is clicked.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: TangledDemo';
$to = 'shanaywork#gmail.com';
$subject = 'Hello';
$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>';
}
} else if ($_POST['submit']) {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
I am using MAMP to host the website locally. The problem occurs when the send button is hit, instead of the email being sent a page with the code is shown.
What is wrong in my code and how may I fix it?
Thank You.
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: TangledDemo';
$to = 'shanaywork#gmail.com';
$subject = 'Hello';
$body = "From: $name\n E-Mail: $email\n Message:\n $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>';
}
} else{
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
There are lots of error in your code
The condition in if and in else if are same so the else if condition never execute.
While checking submit from post value use isset($_POST['NAME']).
Related
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.
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.
This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 8 years ago.
Im trying to redirect the user after submitting a form. Im currently using Header("Location:http://www.google.com") as a test, but it stays on my site after submitting.
What am I doing wrong?
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Website';
$to = 'contact#joakimsorensen.se';
$subject = 'Hello';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit'] && $human == '4') {
if (mail ($to, $subject, $body, $from)) {
header("Location: http://www.google.com");
echo 'great';
} else {
echo '<span style="color:red"><p>Something went wrong, go back and try again!</p></span>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<span style="color:red"><p>You answered the anti-spam question incorrectly!</p></span>';
}
?>
It just prints out "great" from the echo.
I think the problem is that the header is already sent. Try to use
?>
<script type="text/javascript">
window.location="http://www.google.com";
</script>
<?php
instead of
header("Location: http://www.google.com");
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.
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