simple contact form for my website

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phpPain
Forum Newbie
Posts: 10
Joined: Mon Nov 24, 2008 6:14 am

simple contact form for my website

Post by phpPain »

Hi,

I am creating a simple contact form, below is my html code:

Code: Select all

 
 
<form method="POST" action="contact.php">
<p style="margin-top: 0;">Fields marked (*) are required</p>
 
<p style="margin-top: 0;">Email From:* <br/>
<input type="text" name="EmailFrom">
<p style="margin-top: 0;">Name:* <br/>
<input type="text" name="Name">
<p style="margin-top: 0;">Address:<br/>
<input type="text" name="Address">
<p style="margin-top: 0;">Telephone:<br/>
<input type="text" name="Telephone">
<p style="margin-top: 0;">Message:*<br/>
<TEXTAREA NAME="Message" ROWS=6 COLS=40>
</TEXTAREA>
<p style="margin-top: 0;"><input type="submit" name="submit" value="Submit">
 
</form>
 
 
 
The PHP code is here:

Code: Select all

 
 
<?php
//martin cooke
 
// get posted data into local variables
 
$EmailFrom = Trim(stripslashes($_POST['EmailFrom'])); 
$EmailTo = " contact@arldrainservices.co.uk";
$Subject = "Message to A R Lane Drain Services";
$Name = Trim(stripslashes($_POST['Name'])); 
$Address = Trim(stripslashes($_POST['Address'])); 
$Telephone = Trim(stripslashes($_POST['Telephone'])); 
$Message = Trim(stripslashes($_POST['Message'])); 
 
 
 
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Address: ";
$Body .= $Address;
$Body .= "\n";
$Body .= "Telephone: ";
$Body .= $Telephone;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
 
// send email 
$success = mail($EmailTo, $Subject, $Body, $Message, "From: <$EmailFrom>");
 
// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.html\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
}
?>
 
 
I just need to know whether this works? I have uploaded it to my hosting provider and it doesn't seem to work. It needs to be tested server side? This way I can understand whether its a problem with my hosting provider or with my code? Please help!

ps just to let you know when a user fills in the form it always displays the error.html page...
User avatar
mrvijayakumar
Forum Commoner
Posts: 58
Joined: Tue Aug 18, 2009 12:39 am
Location: Chennai city, India
Contact:

Re: simple contact form for my website

Post by mrvijayakumar »

Hi phpPain,
This problem arises on contact.php file. I modified your code and adding correct code below,

Note: If u not receiving a mail means disable the following line "$headers .= 'From: <$EmailFrom>' . "\r\n";".

Code: Select all

<?php
//martin cooke
 
// get posted data into local variables
 
$EmailFrom = trim(stripslashes($_POST['EmailFrom']));
$EmailTo = "contact@arldrainservices.co.uk";
$Subject = "Message to A R Lane Drain Services";
$Name = trim(stripslashes($_POST['Name']));
$Address = trim(stripslashes($_POST['Address']));
$Telephone = trim(stripslashes($_POST['Telephone']));
$Message = trim(stripslashes($_POST['Message']));
 
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
$headers .= 'From: <$EmailFrom>' . "\r\n";
 
$Message = "
Name:$Name 
Address: $Address 
Telephone: $Telephone 
$Message";
 
// send email
$success = mail($EmailTo, $Subject, $Message, $headers);
 
// redirect to success page
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.html\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
}
?>
phpPain
Forum Newbie
Posts: 10
Joined: Mon Nov 24, 2008 6:14 am

Re: simple contact form for my website

Post by phpPain »

Excellent, it now works, thankyou very much. :D
User avatar
mrvijayakumar
Forum Commoner
Posts: 58
Joined: Tue Aug 18, 2009 12:39 am
Location: Chennai city, India
Contact:

Re: simple contact form for my website

Post by mrvijayakumar »

Welcome dear...
Post Reply