Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-14-2012, 09:35 PM   PM User | #1
Mehdi72
New Coder

 
Join Date: Jul 2007
Posts: 88
Thanks: 12
Thanked 0 Times in 0 Posts
Mehdi72 is an unknown quantity at this point
Question Contact Form Success Message on contact form not in new window?

I've followed a tutorial and created a contact form. Once the form has been submitted the browser opens a new page displaying a success message. How do I display the message at the bottom of the contact form itself? I think I need to declare a variable in php then call the variable in the HTML but don't know how to do it. I want to keep the php and html as separate pages.

PHP Code:
<?php
if(isset($_POST['email'])) {
 
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to "email@email.com";
$email_subject "Express Travel Booking";
 
$Name $_POST['Name']; // required
$email_from $_POST['email']; // required
$Telephone $_POST['Telephone']; // not required
$PickUp $_POST['PickUp']; // required
$DropOff $_POST['DropOff']; // required
$Passengers $_POST['Passengers']; // required
$Date $_POST['Date']; // required
$Time $_POST['Time']; // required
$Comments $_POST['Comments']; // required
 
$email_message "Form details below.\n\n";
 
function 
clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return 
str_replace($bad,"",$string);
}
 
$email_message .= "Name : ".clean_string($Name)."\n";
$email_message .= "email address : ".clean_string($email_from)."\n";
$email_message .= "Telephone : ".clean_string($Telephone)."\n";
$email_message .= "Pick Up : ".clean_string($PickUp)."\n";
$email_message .= "Drop Off : ".clean_string($DropOff)."\n";
$email_message .= "Passengers : ".clean_string($Passengers)."\n";
$email_message .= "Date : ".clean_string($Date)."\n";
$email_message .= "Time : ".clean_string($Time)."\n\n";
$email_message .= "Comments : \n\n".clean_string($Comments)."\n";
// create email headers
$headers 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' phpversion();
@
mail($email_to$email_subject$email_message$headers);
?>
 
<!-- include your own success html here -->
 
Thank you for your enquiry. We will be in touch soon.
 
<?php
}
?>
Code:
<form method="post" action="sendmail.php">

<fieldset>
<legend>Contact Form</legend>

<div class="Form">
<label for="Name">Name</label><input type="text" class="inset" name="Name" id="Name" accesskey="1" title="enter name" />
<label for="email">email address</label><input type="text" class="inset" name="email" id="email" accesskey="2" title="enter email address" />
<label for="Telephone">Telephone</label><input type="text" class="inset" name="Telephone" id="Telephone" accesskey="3" title="enter Telephone Number" />
<label for="PickUp">Pick Up</label><input type="text" class="inset" name="PickUp" id="PickUp" accesskey="4" title="enter Pick Up Destination" />
<label for="DropOff">Drop Off</label><input type="text" class="inset" name="DropOff" id="DropOff" accesskey="5" title="enter Drop Off Destination" />
<label for="Passengers">Passengers</label><input type="text" class="inset" name="Passengers" id="Passengers" accesskey="5" title="enter number of Passengers" />
<label for="Date">Date</label><input type="text" class="inset" name="Date" id="Date" accesskey="6" title="enter Date" />
<label for="Time">Time</label><input type="text" class="inset" name="Time" id="Time" accesskey="7" title="enter Time" />
<label for="Comments">Comments</label>
<textarea id="Comments" name="Comments" class="comment"></textarea>

<div class="submit"><input name="Submit" type="submit" class="postc" value="Send"></div>
<div class="submit"><input name="Reset" type="reset" class="reset" value="Reset"/></div>
</div>
</fieldset>
</form>
Mehdi72 is offline   Reply With Quote
Old 10-14-2012, 10:38 PM   PM User | #2
Redcoder
Regular Coder

 
Redcoder's Avatar
 
Join Date: May 2012
Location: /dev/couch
Posts: 309
Thanks: 2
Thanked 46 Times in 45 Posts
Redcoder has a little shameless behaviour in the past
To keep the PHP and HTML pages separate is easy. Just use the require_once or include_once PHP function and convert the file to a .php file. It will be one script, but different files.

Sendmail.php

PHP Code:
<?php
if(isset($_POST['email'])) {
 
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to "email@email.com";
$email_subject "Express Travel Booking";
 
$Name $_POST['Name']; // required
$email_from $_POST['email']; // required
$Telephone $_POST['Telephone']; // not required
$PickUp $_POST['PickUp']; // required
$DropOff $_POST['DropOff']; // required
$Passengers $_POST['Passengers']; // required
$Date $_POST['Date']; // required
$Time $_POST['Time']; // required
$Comments $_POST['Comments']; // required
 
$email_message "Form details below.\n\n";
 
function 
clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return 
str_replace($bad,"",$string);
}
 
$email_message .= "Name : ".clean_string($Name)."\n";
$email_message .= "email address : ".clean_string($email_from)."\n";
$email_message .= "Telephone : ".clean_string($Telephone)."\n";
$email_message .= "Pick Up : ".clean_string($PickUp)."\n";
$email_message .= "Drop Off : ".clean_string($DropOff)."\n";
$email_message .= "Passengers : ".clean_string($Passengers)."\n";
$email_message .= "Date : ".clean_string($Date)."\n";
$email_message .= "Time : ".clean_string($Time)."\n\n";
$email_message .= "Comments : \n\n".clean_string($Comments)."\n";
// create email headers
$headers 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' phpversion();
if(@
mail($email_to$email_subject$email_message$headers))
{
//if email sending is successful
//Note the below message variable. It will be echoed at the bottom of the form
$complete_message 'Thank you for your enquiry. We will be in touch soon.';
 }
}
?>
So, form.php will be :

PHP Code:
<?php

require_once('sendmail.php');
//Same as copy-pasting sendmail.php code at this point

?>
<form method="post">

<fieldset>
<legend>Contact Form</legend>

<div class="Form">
<label for="Name">Name</label><input type="text" class="inset" name="Name" id="Name" accesskey="1" title="enter name" />
<label for="email">email address</label><input type="text" class="inset" name="email" id="email" accesskey="2" title="enter email address" />
<label for="Telephone">Telephone</label><input type="text" class="inset" name="Telephone" id="Telephone" accesskey="3" title="enter Telephone Number" />
<label for="PickUp">Pick Up</label><input type="text" class="inset" name="PickUp" id="PickUp" accesskey="4" title="enter Pick Up Destination" />
<label for="DropOff">Drop Off</label><input type="text" class="inset" name="DropOff" id="DropOff" accesskey="5" title="enter Drop Off Destination" />
<label for="Passengers">Passengers</label><input type="text" class="inset" name="Passengers" id="Passengers" accesskey="5" title="enter number of Passengers" />
<label for="Date">Date</label><input type="text" class="inset" name="Date" id="Date" accesskey="6" title="enter Date" />
<label for="Time">Time</label><input type="text" class="inset" name="Time" id="Time" accesskey="7" title="enter Time" />
<label for="Comments">Comments</label>
<textarea id="Comments" name="Comments" class="comment"></textarea>

<div class="submit"><input name="Submit" type="submit" class="postc" value="Send"></div>
<div class="submit"><input name="Reset" type="reset" class="reset" value="Reset"/></div>
</div>
</fieldset>
</form>
<?php
echo $complete_message//The thank you message will be echoed if the emailing is successful in sendmail.php
?>
By leaving out the form action attribute completely, the form will be submitted to the same page where it will be processed by the sendmail.php code.
__________________
For professional Hosting and Web design.....


NetEssentials.co.uk

Last edited by Redcoder; 10-14-2012 at 10:42 PM..
Redcoder is offline   Reply With Quote
Users who have thanked Redcoder for this post:
Mehdi72 (10-15-2012)
Old 10-14-2012, 11:25 PM   PM User | #3
Mehdi72
New Coder

 
Join Date: Jul 2007
Posts: 88
Thanks: 12
Thanked 0 Times in 0 Posts
Mehdi72 is an unknown quantity at this point
Thanks for the reply. Sorted!

Last edited by Mehdi72; 10-14-2012 at 11:30 PM..
Mehdi72 is offline   Reply With Quote
Old 10-14-2012, 11:35 PM   PM User | #4
Redcoder
Regular Coder

 
Redcoder's Avatar
 
Join Date: May 2012
Location: /dev/couch
Posts: 309
Thanks: 2
Thanked 46 Times in 45 Posts
Redcoder has a little shameless behaviour in the past
Change the action of the form from:

Code:
<form method="post" action="sendmail.php">
To:

PHP Code:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Or:

PHP Code:
<form method="post" action ="<?= $_SERVER['REQUEST_URI']; ?>" >
Or:

PHP Code:
<form method="post" action=""
The above different code types will make it refer to this script(same script).

Correcting my earlier comment. Do not delete the action attribute. It is a mandatory attribute.

You should move the require_once to the top of the PHP script to prevent confusion in case you expand your script later.
__________________
For professional Hosting and Web design.....


NetEssentials.co.uk

Last edited by Redcoder; 10-14-2012 at 11:44 PM..
Redcoder is offline   Reply With Quote
Users who have thanked Redcoder for this post:
Mehdi72 (10-15-2012)
Old 10-15-2012, 02:15 AM   PM User | #5
Mehdi72
New Coder

 
Join Date: Jul 2007
Posts: 88
Thanks: 12
Thanked 0 Times in 0 Posts
Mehdi72 is an unknown quantity at this point
I've put the action attribute back in although the form was sending data without it. It works and validates! I have another unrelated php issue but will post another thread.

Thanks for the help. If you need a HTML5 or CSS3 template or any help with SEO let me know.

Last edited by Mehdi72; 10-15-2012 at 02:37 AM..
Mehdi72 is offline   Reply With Quote
Old 10-15-2012, 09:59 AM   PM User | #6
Redcoder
Regular Coder

 
Redcoder's Avatar
 
Join Date: May 2012
Location: /dev/couch
Posts: 309
Thanks: 2
Thanked 46 Times in 45 Posts
Redcoder has a little shameless behaviour in the past
Quote:
Originally Posted by Mehdi72 View Post
I've put the action attribute back in although the form was sending data without it. It works and validates! I have another unrelated php issue but will post another thread.

Thanks for the help. If you need a HTML5 or CSS3 template or any help with SEO let me know.
Sure. Glad I could help.

__________________
For professional Hosting and Web design.....


NetEssentials.co.uk
Redcoder is offline   Reply With Quote
Reply

Bookmarks

Tags
php contact form

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:47 PM.


Advertisement
Log in to turn off these ads.