My web hosting company says there is something wrong with PHP code - php

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

Related

PHP Contact Form return code [duplicate]

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']).

Redirect after form submit PHP [duplicate]

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");

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 with form alert box

I would like this php form to pop up an alert box on completion or if there is error.
What do I need to add or change to this to make that happen, or is that added javascript?
<?php
$name = $_POST['fullname'];
$email = $_POST['email'];
$message = $_POST['comment'];
$from = 'From: Contact Form';
$to = 'email#domain.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>';
}
?>
To do this you will need to add some more technologies, specifically Javascript and AJAX.
PHP is a sever-side language, which means that by the time the browser is rendering the page PHP has completed running your code.
In the page that would display the error, you would add javascript to pop up the message using the alert() function. You could use php to conditionally output this javascript. Oy vey.
Example:
<script language='javascript'>
<?php if ($error) { ?>
window.onload = function() {
alert('<?php echo $error?>');
}
<?php } ?>
</script>
Something like that.

Categories