E-mailing forms and headers!
Posted: Thu Oct 01, 2009 12:57 pm
Hi, I have recently built a website for the first time and agreed to add a feedback form on the contact page.
I tried using some of those form wizards, but they were very messy and never produced the results I wanted. So I decided to learn how to code php from scratch (with no prior coding experience other than html).
I am having two major issues. The first is using the header function to cache the browsers. This is because if i get an error the first time the form goes, even when I fill it in correctly, there is still an error. I have got rid of all html before it and it is the first thing in the php script. I do not know what to do?
Secondly is using the header function within the script. I realise that you cannot send headers once html has been outputted. But I believe you can use something called output buffers? I have tried this and all it has done is heavily confuse me!!! This is why all I have at the moment is an echo of 'error' or 'done' and have commented out the header functions in the script.
Thankyou very much if you've read this far!!!
Regards
Sam
Here is my script:
I tried using some of those form wizards, but they were very messy and never produced the results I wanted. So I decided to learn how to code php from scratch (with no prior coding experience other than html).
I am having two major issues. The first is using the header function to cache the browsers. This is because if i get an error the first time the form goes, even when I fill it in correctly, there is still an error. I have got rid of all html before it and it is the first thing in the php script. I do not know what to do?
Secondly is using the header function within the script. I realise that you cannot send headers once html has been outputted. But I believe you can use something called output buffers? I have tried this and all it has done is heavily confuse me!!! This is why all I have at the moment is an echo of 'error' or 'done' and have commented out the header functions in the script.
Thankyou very much if you've read this far!!!
Regards
Sam
Here is my script:
Code: Select all
<?php
header( "Expires: Mon, 20 Dec 1998 01:00:00 GMT" );
header( "Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT" );
header( "Cache-Control: no-cache, must-revalidate" );
header( "Pragma: no-cache" );
// retrieve form data
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$company_name = $_POST['company_name'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$enquiry = $_POST['enquiry'];
$message = "Name:".' '.$first_name.' '.$last_name."\n"."Company Name:".' '.$company_name."\n"."E-mail:".' '.$email."\n"."Telephone no.:".' '.$telephone."\n"."Enquiry:".' '.$enquiry."\n";
//spamming
if ( preg_match( "/[\r\n]/", $first_name ) || preg_match( "/[\r\n]/", $last_name ) || preg_match( "/[\r\n]/", $company_name ) || preg_match( "/[\r\n]/", $email ) || preg_match( "/[\r\n]/", $telephone ) || preg_match( "/[\r\n]/", $enquiry ) ) {
echo "error";
//header( "Location:http://change-abilityconsulting.com/site_upload/6_contact/error.html" );
}
//stop empty fields
elseif(empty($first_name) || empty($last_name) || empty($email) || empty($telephone) || empty($enquiry)){
echo "error";
//header( "Location:http://change-abilityconsulting.com/site_upload/6_contact/error.html" );
}
else{
//print form data
mail( "INSERT_TEST_E-MAIL", "Feedback Form Results", $message, "From: $email" );
echo "done";
//header( "Location:http://change-abilityconsulting.com/site_upload/6_contact/thankyou.html" );
}
?>