Jump to content

Simple PHP contact form


fathernugen

Recommended Posts

Hi,

 

Today I have taken my first step towards familiarisation with php.

 

I have created a simple form form a tutorial I read.

 

The mail.php file is this:

 

<?php
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$dropdown = $_POST['dropdown'];
$formcontent=" From: $firstname \n Surname: $lastname \n Email: $email \n Dropdown: $dropdown";
$recipient = "fathernugen@googlemail.com";
$subject = "Newsletter Sign Up";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "You have been added to our newsletter subscription list!" . " -" . "<a href='newsletter.php' style='margin:0 auto; top: 100px; text-decoration:none;color:#ff0099;'> Return to Sitting Spiritually</a>";
?>

 

My problem is that users are redirected to a blank page with 'thank you return to sitting spiritually'.

 

I would like to use a custom thank you page with a url like;

 

www.sittingspiritually.co.uk/thankyou.php

 

so the look of the site is not completely ruined by my php inadequacies.

 

 

Is there a small piece of code i can add to the mail.php file so i can use a custom page for the success???

 

I hope some one can help, thanks in advance.

 

 

Link to comment
Share on other sites

Yes, you can use an if statement with the mail function, to forward users to a thank you page upon mail success. (Mail function returning true: )

 

if ( mail($recipient, $subject, $formcontent, $mailheader) ){
header('Location: http://www.mysite.com/thankyou.php');
} else {
die ("error");
}

Link to comment
Share on other sites

Or just replace:

 

echo "You have been added to our newsletter subscription list!" . " -" . "<a href='newsletter.php' style='margin:0 auto; top: 100px; text-decoration:none;color:#ff0099;'> Return to Sitting Spiritually</a>";

With:

 

header('Location: http://www.sittingspiritually.co.uk/thankyou.php');
exit;

 

Link to comment
Share on other sites

I have added the code Abracadaver posted and it works.

 

Sweet, thanks.

 

But how do I tackle the failure which snowman is talking about.

 

Also which piece of code is the best practice? Abracadavers or Snowmans???

 

I do have a simple jquery validation which stops the form being sent if the field are not filled.

 

Take a look at www.sittingspiritually.co.uk/newsletter.php

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.