Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Change Location

8:07 am on Dec 10, 2013 (gmt 0)

Full Member

10+ Year Member

joined:Feb 24, 2002
posts: 327
votes: 0

I've used HTML and JavaScript for years and am finally trying PHP. There are a gazillion tutorials on the web telling you how to code a simple form on a webpage that sends the input in an e-mail.

The problem is that they all end up with a blank white screen and a PHP print message like: Form Sent

They all do that, but it's so lame. Who wants to end up with a black screen and no links! You should end up on a page of your website.

So I'm trying to direct it to the URL of one of my webpages, and inserted the code you see below. I understand it is failing because you can only use header location at the start of your php file. But you don't want to change the location before executing the php?

Thank you, Peter

$subject="Membership Application";
$FullName="$FirstName $LastName";
$message="First Name: $FirstName\nLast Name: $LastName";
$headers="From: $FullName";
if ($sent) {header("Location: sent.html");
else {header("Location: failed.html");
9:50 am on Dec 11, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Oct 15, 2004
votes: 0

well, if you delete the <html><body> and </body></html> part your script will work

have your form post to a pure php page - no html there.
validate the fields
save to db
send email
at the end, if all are ok, use the header() to redirect to a thank you page (pure html page)

[edited by: omoutop at 9:54 am (utc) on Dec 11, 2013]

9:53 am on Dec 11, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Oct 15, 2004
votes: 0

alternative, since you are familiar with javascript (and perhaps some framework like jquery?) you can:

- submit form with ajax
- if success replace form container with a thank you message
- if failed, show error and keep form on page for user to correct
11:26 am on Dec 11, 2013 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
votes: 348

You can keep the <html><body> et cetera if you make a page to go with it.

if (long string of conditions covering all the things that can go wrong with user input such as malformed e-mail address, missing text et cetera)
echo (wide array of messages telling human user they did something wrong)
else (pretty thank-you text)

and then the rest of your every-page boilerplate winding up with </body></html>
12:01 pm on Dec 23, 2013 (gmt 0)

New User

joined:Dec 23, 2013
posts: 2
votes: 0

remove the html tags and save as a php page and validate the form using javascript or jquery if the credentials are valid then redirect to thank you message page or show thank you div using jquery before hide that div using jquery in same page, if the credentials are invalid show error message using jquery near to email textbox.
James mad
connected commerce