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.
Related
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']).
Okay so I made a form on a website so that someone can write down their information and submit a message to an email. It then redirects them back to the homepage of the site, the problem I'm having is that it doesn't show when the message has sent, it just takes me back to the homepage automatically. How can I put a small delay so that it will show that the email was sent? Maybe I need to make it pop in a message box instead? It's been a while since I've worked with php so I'm a bit rusty.
<?php
ob_start();
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$from = 'From: Subject';
$to = 'myemailhere#example.com';
$subject = 'Subject';
$body = "From: $name\n E-Mail: $email\n Phone: $phone\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>';
}
}
$url = 'http://example.com';
while(ob_get_status())
{
ob_end_clean();
}
header("Location: $url");
?>
Also I replaced a few things with generic information, sorry guys you're not getting my email.
You can't easily do this on the server side. The best thing to do is return a page which contains the message, and redirects shortly after, either via javascript or META tags.
See answer at Redirect website after certain amount of time
You can only send the redirect header before any html is output. Have you've considered using a timed meta html redirect?
<meta http-equiv="refresh" content="5; url=http://homepage.com/">
or js:
window.location.href = "http://homepage.com";
Note: You can also instead of redirecting, let the user know that the form has been submitted successfully, and link them to the page they were prior.
Eg: Thank you for your submission, click here to return to our website.
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 have a PHP form. The form works, and can send emails through it. It doesn't look like it's sending them from a specific email address to the email address I want the emails to go to (xxx#a.com).
I would like these emails to be sent from yyy#a.com, which I have configured below. Here is the PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Contact';
$to = 'me#a.com';
$subject = 'Contact';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo '<script type="text/javascript">
alert("Your message has been sent!");
</script>';
} else {
echo '<script type="text/javascript">
alert("Something went wrong, try again.");
</script>';
}
}
?>
I have tried changing the $from to yyy#a.com, but that doesn't change the email's from address. Why isn't the From address getting set?
The fourth parameter isn't from, it is extra headers. So to include an extra from header, do this:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Contact <yyy#a.com>'; // You can combine name and address
$to = 'xxx#a.com';
$subject = 'Contact';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$extraHeaders = 'From:'.$from; // Header field + header field value.
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $extraHeaders)) { // Pass the extra headers...
echo '<script type="text/javascript">
alert("Your message has been sent!");
</script>';
} else {
echo '<script type="text/javascript">
alert("Something went wrong, try again.");
</script>';
}
}
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