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");
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I'm trying to set up a contact me page and currently I've got it working all the way to the point of the 'message sent' result showing up correctly. However the e-mail never shows up for me in my box inbox.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$antispam = $_POST['antispam'];
$to = 'myemailaddress#gmail.com';
$from = 'From : ' . $email;
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($antispam == '10' || 'Ten' || 'ten') {
$human = true;
}
if($_POST['submit'] && $name != "" && $email != "" && $message != "" && $subject != "") {
if ($human == true) {
if (mail($to, $subject, $body, $from)) {
$result = "Your message was sent successfully!";
} else {
$result = "Something went wrong! Try sending your message again";
}
} else {
$result = "You answered the anti-spam answer incorrectly. Please try again.";
}
} else {
$result = "You did not fill out a required field. Please try again.";
}
?>
<?php echo $result; ?>
I've read seperately that Gmail has issues with PHP mail(), is that possibly the cause?
I found the answer and honestly feel dumber for not recognizing it.
It was never told to send the email, just check if sending it would be true or not. I know that sounds weird but here's the old code:
if ($human == true) {
if (mail($to, $subject, $body, $from)) {
$result = "Your message was sent successfully!";
} else {
$result = "Something went wrong! Try sending your message again";
}
and here's the fixed version
if ($human == true) {
mail($to, $subject, $body, $from);
if (mail($to, $subject, $body, $from)) {
$result = "Your message was sent successfully!";
} else {
$result = "Something went wrong! Try sending your message again";
}
to be short, I added mail($to, $subject, $body, $from); after the check to see if $human == true
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.
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']).
This is the PHP it suppose to connect to a contact.html page and then a thank you page after afterwards.I just wanted a contact form. it wont recognize files as .php.I did save it like that.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['number'];
$message = $_POST['message'];
$from = 'From:you';
$to = 'me#hotmail.com';
$subject = 'Hello';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\n Number: $number\n Message:\n $message";
if ($_POST['submit']) {
if ($name != '' && $email != '') {
if ($human == '4') {
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>You answered the anti-spam question incorrectly!</p>';
}
} else {
echo '<p>You need to fill in all required fields!!</p>';
}
header("Location: thanks.html");
}
?>
Assuming that you wish to run this from a form, you will need to set your HTML form tag as follows:
<form action="contact.php" method="post">
You should then rename contact.html to contact.php (any text editor should be able to do this easily).
Finally, you're using PHP's header() function, which will cause errors if you have output to the browser before it is called. This includes using PHP's echo struct. Your contact.php file should look like this (and be in the same directory as your HTML file containing the form):
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['number'];
$message = $_POST['message'];
$from = 'From:you';
$to = 'me#hotmail.com';
$subject = 'Hello';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\n Number: $number\n Message:\n $message";
if ($_POST['submit']) {
if ($name != '' && $email != '')
{
if ($human == '4')
{
if (mail ($to, $subject, $body, $from))
{
header("Location: thanks.html");
}
else
{
echo '<p>Something went wrong, go back and try again!</p>';
}
}
else
{
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
}
else
{
echo '<p>You need to fill in all required fields!!</p>';
}
}
?>
Note: I fixed your layout a little and changed some of the conditions that you were using. The first elseif was actually redundant, and an else will suffice.
You wrote that it's a file named contact.html or you try to connect to contact.html - is this correct? You should rather use contact.php.
PHP will not execute in .html file extensions without server configuration (for example, a directive in a .htaccess file if you're using Apache).
Since you appear to have a managed web hosting account, you may not be able to set this yourself. If you would like it, I would suggest asking your hosting provider. If not, renaming the file to have a .php file extension should work.
Try to check out if php is working:
<?php phpinfo(); ?>
If you see no output, your php-code won't be parsed, maybe because your contact-site has an *.html ending, it seemingly needs a *.php file ending, to be parsed,
if you want to use the extension '*.html', you have to add some lines to your webserver - configuration file,
have a look at this: maybe a solution
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