Semi-newbie question about PHP Mail Form script

They have: 19 posts

Joined: May 2009

I'm semi-new to PHP, but I understand most of the basics, how variables work, etc. I'm using the "Free Secure PHP Mail Form" from jemjabella.co.uk. I've used this on other sites and it works great...have been able to make any modifications I need...until now.

The way it's set up is that the form action is directed back at the same page the form is on, and any error or success messages are delivered in the context of that page.

The problem: I need to modify this so that when the form is submitted successfully (no errors), the visitor is redirected to another "Thank You" page where they can also download a free PDF report. Any ideas where to start with this? I can't figure it out.

The script in question can be found here:
http://www.jemjabella.co.uk/downloads/mail_form_v2.txt

If I'm not being clear enough, please let me know and I'll provide more info. Thanks!

Tim

greg's picture

He has: 1,581 posts

Joined: Nov 2005

The problem with POSTing data to a page where headers are sent (something is output to the browser like the <title> and form) is it's stored in the users browser. So when the user clicks refresh, or navigates away from the page then back to the same page by using the browser back button, the POST data is sent again.

In this case, then email may be sent again, or fail with the same reasons.
The best approach is to have three pages. The one with the form and error message text, and another where the data is POSted to which checks all the data and sends an email if ok, or back to the form page with messages if not.
The third is where the users goes if the email (2nd page) was sent.

With the script you have, as the page sends headers (page title etc) you can't use a PHP redirect without errors. If error level is low enough on your server it will work and either save the error to the log or ignore it. But it's not good practice as if you ever move to a server where error reporting level is higher the script will halt.

I've read about Javascript redirects, but I don't use JS so I can't tell you much about that (someone else will no doubt).

With the three page scenario as I mentioned above, the second page has no headers sent (output to the browser) so you can send the email and then send them to the 3rd page with "Thank you ..etc" and give them the PDF link.
Or of course back to the form if there were any errors.

The code you want is exit(header('Location: yourpage.php'));. And probably replace your current code of echo '<p>Your mail was successfully sent.</p>';. So it sends them to your new page when successful.

But using your current script all on one page the header redirect will cause an error, and possibly issues. It's not ideal with your current script setup.

They have: 19 posts

Joined: May 2009

Hi Greg,

Thanks for that thorough response. I think I am basically understanding the concept here, but I'm not totally certain I understand the execution. As I said, I'm still relatively new to PHP so I'm not sure I know how and where to put some of the things you mentioned.

Is the "exit" code what I would use for any kind of redirect?...So I would replace the "echo" part that you mentioned with that exit code in order to go to a different page instead of just echoing the success message?

Also, a couple of other things...

1. Here is a link with the actual page I am working on, I put it up temporarily at my site:
http://www.tim-webb.com/marketedge/contact-us.php

Currently the successful form submissions are sent to my inbox.

2. There is one other possibility here that I've thought of...instead of displaying the "Your mail was successfully sent" line on the same page, is there a way to display the following instead, including link?...

"Thank you.

Your email has been sent and we will get back to you shortly.

Click here to view the article "25 Surefire Ways to Grow Profit - Guaranteed!" "

Where the "click here" is of course a link to the PDF being offered. Is this possible? Initially I thought I could put HTML code with link inside the echo command but that didn't work at all. The point is, we don't actually NEED to go to another page, if there is a way to display a text link in the error/success area on that same page. Bottom line, when someone submits the form successfully, they need to be able to get to that PDF.

Does this make sense?

Thanks!!!
Tim

They have: 19 posts

Joined: May 2009

Okay, I think I've solved it...as I said in my last post I was working towards the simpler solution of having the link to the PDF just show up on the same page as the form, in the same area as the error/success messages. This turned out to be pretty easy, again pointing to my lack of knowledge of PHP.

Every time I tried to put the link into the echo (with a href) it gave me errors...I finally realized this was because the quotes in the a href were conflicting with the other quotes at the beginning and ending of the echoed part. When I changed the outer quotes to single quotes, it worked.

I think I'm slowly starting to get the hang of this...

greg's picture

He has: 1,581 posts

Joined: Nov 2005

That's pretty much the path I went when I started, and I guess the same for most people.
Edit and alter some code, see what the changes do and learn from it.

When you fill out the form and get your "thank you..successful" message, what happens when you press page refresh? Does it send the email again?
And every time you press refresh, or navigate away from the page and click browser back to that page?

They have: 19 posts

Joined: May 2009

Hi Greg,

Sorry for the late response, I've been away from the forum for a bit! Thanks again for all your help.

I don't actually know what happens when the page is refreshed, that's a good question & I didn't think to test it...I should probably check that out!

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.