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 = "[email protected]";
$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
https://forums.phpfreaks.com/topic/224325-simple-php-contact-form/
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");
}

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;

 

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

 

 

The only problem with the above code is that it does not account for mail failure. Either one will work though. I would try to start your better php habits now though.

 

They already have:

 

mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.