Forms

Soldato
Joined
5 Aug 2004
Posts
7,386
Location
North East England
Hey,

Does anyone have a good guide or information on how to create a data entry form on a website and when you press submit it doesn't open a 3rd party email program like outlook, it just sends it straight away?

Cheers.
 
Can't get it to work :(

www.gogreendrivingschool.com/contact.html

Code:
<form method="POST" action="contact.php">
Fields marked (*) are required

<p>Email From:* <br>
<input type="text" name="EmailFrom">
<p>Salutation:<br>
<input type="text" name="Salutation">
<p>FirstName:<br>
<input type="text" name="FirstName">
<p>LastName:<br>
<input type="text" name="LastName">
<p>PostCode:<br>
<input type="text" name="PostCode">
<p>HomeTel:<br>
<input type="text" name="HomeTel">
<p>Mobile:<br>
<input type="text" name="Mobile">
<p><input type="submit" name="submit" value="Submit">
</form>

Code:
// get posted data into local variables
$EmailFrom = Trim(stripslashes($_POST['EmailFrom'])); 
$EmailTo = "[email protected]";
$Subject = "Driving Lesson Enquiry";
$Salutation = Trim(stripslashes($_POST['Salutation'])); 
$FirstName = Trim(stripslashes($_POST['FirstName'])); 
$LastName = Trim(stripslashes($_POST['LastName'])); 
$PostCode = Trim(stripslashes($_POST['PostCode'])); 
$HomeTel = Trim(stripslashes($_POST['HomeTel'])); 
$Mobile = Trim(stripslashes($_POST['Mobile'])); 

// validation
$validationOK=true;
if (Trim($EmailFrom)=="") $validationOK=false;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
  exit;
}

// prepare email body text
$Body = "";
$Body .= "Salutation: ";
$Body .= $Salutation;
$Body .= "\n";
$Body .= "FirstName: ";
$Body .= $FirstName;
$Body .= "\n";
$Body .= "LastName: ";
$Body .= $LastName;
$Body .= "\n";
$Body .= "PostCode: ";
$Body .= $PostCode;
$Body .= "\n";
$Body .= "HomeTel: ";
$Body .= $HomeTel;
$Body .= "\n";
$Body .= "Mobile: ";
$Body .= $Mobile;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.htm\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>

Just brings up an error, not really sure what i'm doing. I copied that code from a form generator aswell :(

Cheers.
 
Does your webserver have PHP installed because when I try and POST the form all I get is the PHP script in raw text format. Could also be caused by an incorrectly configured webserver with PHP installed.
 
Yep it does, Surpass Hosting. Has always had PHP.

Do I need to set it as PHP in the index.html or something?
 
Doh! silly typo, should have spotted it earlier, add '<'?php' and '?>' around your code, it's just plaintext at the moment, no wonder it isn't executing, for example:

Code:
<?php

code goes here

?>
 
Last edited:
Doh! silly typo, should have spotted it earlier, add '<'?php' and '?>' around your code, it's just plaintext at the moment, no wonder it isn't executing, for example:

Code:
<?php

code goes here

?>

Still doesn't work :(

ZomgERROR said:
The requested URL /error.htm was not found on this server.
 
Hey mate, i've broke it again T_T;;;

I've tried to add a text area, as you can see at http://www.gogreendrivingschool.com/contact.html and I get an error.

I've tried adding the variables to the php file but it's having none of it, sorry for being a pain :p

Code:
				<form method="POST" action="contact.php">
				Fields marked (*) are required
				
				<p>FirstName:<br>
				<input type="text" name="FirstName">
				<p>LastName:<br>
				<input type="text" name="LastName">
				<p>PostCode:<br>
				<input type="text" name="PostCode">
				<p>HomeTel:<br>
				<input type="text" name="HomeTel">
				<p>Mobile:<br>
				<input type="text" name="Mobile">
				<p>Comments:<br>
				<textarea cols="50" rows="4" name="Comment">Do you have your provisional? Previous experience? When is best to contact you?</textarea>
				<p><input type="submit" name="submit" value="Submit">
				</form>

Code:
<?php
// get posted data into local variables
$EmailTo = "[email protected]";
$Subject = "Driving Lesson Enquiry";
$FirstName = Trim(stripslashes($_POST['FirstName'])); 
$LastName = Trim(stripslashes($_POST['LastName'])); 
$PostCode = Trim(stripslashes($_POST['PostCode'])); 
$HomeTel = Trim(stripslashes($_POST['HomeTel'])); 
$Mobile = Trim(stripslashes($_POST['Mobile'])); 
$Comment = Trim(stripslashes($_POST['Comment'])); 

// validation
$validationOK=true;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
  exit;
}

// prepare email body text
$Body = "";
$Body .= "FirstName: ";
$Body .= $FirstName;
$Body .= "\n";
$Body .= "LastName: ";
$Body .= $LastName;
$Body .= "\n";
$Body .= "PostCode: ";
$Body .= $PostCode;
$Body .= "\n";
$Body .= "HomeTel: ";
$Body .= $HomeTel;
$Body .= "\n";
$Body .= "Mobile: ";
$Body .= $Mobile;
$Body .= "\n";
$Body .= "Comment: ";
$Body .= $Comment;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body);

// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=success.htm\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
 
It comes back sucess for me, have you updated it since you posted?

Nope? :confused:

Possibly be my cache or something? I'll ctrl + f5 it and see what happens.

Edit: Yeah, seems to work now. Wierd :o Sorry :p
 
Last edited:
You might want to do some validation on the fields, I noticed that the validation check is hard coded to 'true' in your script, you might want to alter it so people can't spam empty messages. You will still probably get spam from bots and may have to add a captcha if it gets out of control (which it may do as your site is linked to and the google rank goes up).

So instead of:
Code:
// validation
$validationOK=true;

You need to check the field contents so that an empty form isn't being sent.
 
Back
Top Bottom