...

View Full Version : Contact Form Success Message on contact form not in new window?



Mehdi72
10-14-2012, 09:35 PM
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
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
}
?>



<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>

Redcoder
10-14-2012, 10:38 PM
To keep the PHP and HTML pages separate is easy. Just use the require_once (http://williamthedev.co.cc/?p=49) or include_once (http://williamthedev.co.cc/?p=49) PHP function and convert the file to a .php file. It will be one script, but different files.

Sendmail.php



<?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

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 (http://stackoverflow.com/questions/1131781/is-it-a-good-practice-to-use-an-empty-url-for-a-html-forms-action-attribute-a), the form will be submitted to the same page where it will be processed by the sendmail.php code.

Mehdi72
10-14-2012, 11:25 PM
Thanks for the reply. Sorted!

Redcoder
10-14-2012, 11:35 PM
Change the action of the form from:



<form method="post" action="sendmail.php">


To:



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

Or:



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


Or:



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

Mehdi72
10-15-2012, 02:15 AM
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.

Redcoder
10-15-2012, 09:59 AM
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.

:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum