Pause php to show message before reconnecting - php

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.

Related

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

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

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.

php sending email

mailer.php
<?php
if(isset($_POST['submit'])) {
$to = "abc#gmail.com";
$subject = "Contact via website";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
echo "1";
echo "Data has been submitted to $to!";
mail($to, $subject, $body);
} else {
echo "0";
}
?>
jquery code
$('.submit').click(function(){
$('span.msg').css({'visibility': 'visible'}).text('Sending...');
$.post("mailer.php", $(".contactPage").serialize(),
function(data){
$('span.msg').text((data == "1") ? 'Your Message has been received. Thank you' : "Could not send your message at this time").show();
});
return false;
});
I am always getting
Could not send your message at this time
I have almost no knowledge in php, whats i am doing wrong?
Your php script never prints just 1 it prints 0 or 1Data has been submitted to... thus (data == "1") can never be true.
btw: Your script could at least use the return value of mail() to decide whether it signals success or failure.
First of all, check server's mail settings. If your script resides on a server, which you don't administer, the simplest way is to do this is creating a simple PHP file with the following content:
<?php
mail("youraccount#example.com","test","works"); <br>
?>
Run it and check your e-mail.

php simple contact form not sending mail even after confirmation

I have been trying to get a php contact form working on my portfolio site (currently on a free megabyet.net account), but on testing it(on the uploaded site) even though i get the thankyou/confirmation message, I still don't receive any message on my mail account (specified in the code), I can't seem to understand the problem here....help needed!
can it be something related to SMTP??
Here's the code :
<?php
if(isset($_POST['submit'])) {
$to = "vishu_unlocker#yahoo.com";
$subject = "Portfolio Contact";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $email_field";
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
echo "Mail has been sent, thankyou!";
mail($to, $subject, $body, $headers);
} else {
echo "blarg!";
}
?>
HTML Code:
<form id="contact_frm" action="mail.php" method="POST">
<h4>Name :</h4>
<input type="text" id="f_name" name="name"/><br/><br/>
<h4>E-Mail Address :</h4>
<input type="text" id="f_email" name="email"/><br/><br/>
<h4>Message :</h4>
<textarea id="f_msg" name="message" cols="22" rows="5"/></textarea><br/><br/>
<input id="send_btn" type="submit" value="Send >>" name="submit" /><br/>
</form>
Firstly you should be checking if mail() returns true or not to determine if mail has been sent successfully:
<?php
if(isset($_POST['submit'])) {
$to = "vishu_unlocker#yahoo.com";
$subject = "Portfolio Contact";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $email_field";
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
$success = mail($to, $subject, $body, $headers);
if ($success) {
echo "Mail has been sent, thankyou!";
// redirect to thank you page here
}
else {
echo "message failed";
}
} else {
echo "blarg!";
}
?>
Try that and let us know if that works.
Also, have you tried sending to a different email address? It may be that Yahoo is blocking that web host for spam. Being a free host it is a very likely scenario.
If you are looking for something related to sending email via SMTP. I would recommend you use Code Igniters mailer class.
http://codeigniter.com/user_guide/libraries/email.html
This also allows for debugging and handling SMTP errors gracefully.
can it be something related to SMTP??
Probably. Why don't you check your mailq and the log files from your MTA?
#John .. checked with that if condition with the code below and i get a failed output =/ ...so my mail() function is returning false =( ...and yea i've tried gmail but with the mail function not running fine on the first place.... it doesn't work...
<?php
if(isset($_POST['submit'])) {
$to = "vishu_unlocker#yahoo.com";
$subject = "Portfolio Contact";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $email_field";
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
$success = mail($to, $subject, $body, $headers);
if($success) {
echo "Mail has been sent, thankyou!";
} else {
echo "message sending failed!";
}
} else {
echo "blarg!";
}
?>
output- message sending failed!
so, do I need to define some extra params here?...also i saw that my host has given the path to sendmail as -- /usr/sbin/sendmail does it has anything to do with my mail function acting bad?...i mean do I need to define the sendmail param in it?
#unknown- hmm codeigniter may help, but i've never used it before...let's see...
#symcbean- sorry i don't know how to do that :P...probabaly cuz i'm not very well versed with SMTP yet?.... still a learner/beginner...
If the E-Mail goes out correctly, but never arrives, it could be that it gets caught by a spam filter. A few bullet points I wrote in reply to an similar question a few months ago:
Does the sender address ("From") belong to a domain on your server? If not, make it so.
Is your server on a blacklist (e.g. check IP on spamhaus.org)? This is a remote possibility with shared hosting.
Are mails filtered by a spam filter? Open an account with a freemailer that has a spam folder and find out. Also, try sending mail to an address without a spam filter.
Do you possibly need the fifth parameter "-f" of mail() to add a sender address? (See mail() command in the PHP manual)
If you have access to log files, check those, of course, as suggested above.
Do you check the "from:" address for possible bounce mails ("Returned to sender")? You can also set up a separate "errors-to" address.

Categories