473,387 Members | 1,463 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

HTML Contact Form PHP Code Please Help...

3
Hi all,
I have not got any php knowledge and I have found a php code on the net and using it to send e-mail from a web html form.So far the form and the code works fine.
I have the following php code for a contact form and i would like to add a simple Html thank you page when user submits the form.

Here is the code;

Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. if(isset($_POST['submit'])) { 
  3. $to = "email@e-mail.com"; 
  4. $subject = "Test Message"; 
  5. $name_field = $_POST['fullname']; 
  6. $email_field = $_POST['email']; 
  7. $phone_field = $_POST['phone'];
  8. $message = $_POST['message']; 
  9.  
  10.  
  11. $body ="From:$name_field\nEMail:$email_field\nPhone:$phone_field\nMessage:$message\n"; 
  12.  
  13.  
  14. echo "Your message has been successfully sent $to!"; 
  15. mail($to, $subject, $body); 
  16. } else { 
  17. echo "There was an error sending this message. Please contact us from e-mail@e-mail.com"; 
  18. ?> 
As you can see when the message is sent, a blank page is displayed with the message "Your message has been successfully sent $to!" . Instead of this I need a functionality which will redirect the user to another page; to thank you page a url etc.
Can you please help and modify the code please with some suggestions?
Thank you in advance
ECOT
Nov 9 '09 #1

✓ answered by dlite922

Please use code tags, did you read this before you posted:

http://bytes.com/topic/php/answers/8...-post-question

Anyway,

Use header to redirect. like so
Expand|Select|Wrap|Line Numbers
  1. header('Location: http://www.example.com/');
make sure there is NOTHING before you're opening php tag, not even a space or empty line.

Make sure you do not output anything before that line (no echo or print statements).

It's also better to check to make sure mail() function actually sent it (returns ture) so put it in an if statement.

Here's your revised code. (Note: Don't expect free code, this was fast and simple and more courteous than you usually expect from any forum)

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. if(isset($_POST['submit'])) 
  4. {
  5.     $to = "email@e-mail.com";
  6.     $subject = "Test Message";
  7.     $name_field = $_POST['fullname'];
  8.     $email_field = $_POST['email'];
  9.     $phone_field = $_POST['phone'];
  10.     $message = $_POST['message'];
  11.  
  12.     $body ="From:$name_field\nEMail:$email_field\nPhone:$pho ne_field\nMessage:$message\n";
  13.  
  14.     if(mail($to, $subject, $body))
  15.     {
  16.         header("Location: thankyou.php");
  17.     } 
  18.     else 
  19.     {
  20.         echo "There was an error sending this message. Please contact us from e-mail@e-mail.com";
  21.     }
  22. }
  23. ?>
  24.  
  25.  
  26.  

2 2976
dlite922
1,584 Expert 1GB
Please use code tags, did you read this before you posted:

http://bytes.com/topic/php/answers/8...-post-question

Anyway,

Use header to redirect. like so
Expand|Select|Wrap|Line Numbers
  1. header('Location: http://www.example.com/');
make sure there is NOTHING before you're opening php tag, not even a space or empty line.

Make sure you do not output anything before that line (no echo or print statements).

It's also better to check to make sure mail() function actually sent it (returns ture) so put it in an if statement.

Here's your revised code. (Note: Don't expect free code, this was fast and simple and more courteous than you usually expect from any forum)

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. if(isset($_POST['submit'])) 
  4. {
  5.     $to = "email@e-mail.com";
  6.     $subject = "Test Message";
  7.     $name_field = $_POST['fullname'];
  8.     $email_field = $_POST['email'];
  9.     $phone_field = $_POST['phone'];
  10.     $message = $_POST['message'];
  11.  
  12.     $body ="From:$name_field\nEMail:$email_field\nPhone:$pho ne_field\nMessage:$message\n";
  13.  
  14.     if(mail($to, $subject, $body))
  15.     {
  16.         header("Location: thankyou.php");
  17.     } 
  18.     else 
  19.     {
  20.         echo "There was an error sending this message. Please contact us from e-mail@e-mail.com";
  21.     }
  22. }
  23. ?>
  24.  
  25.  
  26.  
Nov 9 '09 #2
Ecot
3
Thanks a lot mate. It works like a charm. Really appreciate your quick response.
Nov 10 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: josh dismukes | last post by:
/// here is the code i'm getting a parse error on the last line of the code which /// is </html> any help will be much appreciated. <?php session_start ();
17
by: deko | last post by:
This may be a basic question - I have a form on my web page that looks like this: <span id="site_search"> <form name="frmSearch" method="post" action="" style=" margin: 0 0 0 15px "> Search...
1
by: Les Juby | last post by:
A year or two back I needed a search script to scan thru HTML files on a client site. Usual sorta thing. A quick search turned up a neat script that provided great search results. It was fast,...
5
by: jsavagedesign | last post by:
I am trying to send and email from a form using php. I got that to work. The problem is that when I receve the email it is a text version with all the html tags showing. looks like this:...
5
by: NYXX | last post by:
Here is my Html Code and my Contact.php This is My code in my html <td height="345" valign="top"> <form style="margin:0; padding:0; " action="contact.php" method="get" id="form"> <table...
12
by: colt28 | last post by:
Ok so I found an ajax contact form script but i can't get the b****** to work. I made a bunch of alterations to it and it didn't work so i replaced everything with the original and it still didn't...
2
by: preityrao | last post by:
Hi, I am new to perl and was trying to write a perl program witha UI. i.e. I have written a html form which invokes a perl program to execute and to print back the data selected in the form. ...
10
by: happyse27 | last post by:
Hi All, I got this apache errors(see section A1 and A2 below) when I used a html(see section b below) to activate acctman.pl(see section c below). Section D below is part of the configuration...
0
by: studentofknowledge | last post by:
hi my knowledgeable comrads I have created a web form using html, css with the addition of some basic javascript validation. Although now im having trouble to insert this information to my local...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.