[Phpwm] Simple PHP Contact Form

Gavin Kimpson gkimpson at gmail.com
Tue Oct 13 17:47:43 UTC 2009


If you need a simple php form with validation I've got one that I've used
over sometime and modified to match my needs in terms of validation etc..
It is all commented so is very simple to use and understand too, in regards
to your own form i'd agree with jon - best to keep your code as clear as
possible and always use the 'Tab' key - it makes a big difference in reading
the code at a later date. If your new to php security then i'd suggest you
do some reading on 'sql injection' - you'll need to know about this once you
start implementing databases etc..

If you need further info just ask ahead...

On Tue, Oct 13, 2009 at 6:01 PM, Jon Spriggs <jon at spriggs.org.uk> wrote:

> It's not a big thing, but I'd probably do my redirect with a header tag
> rather than a meta-redirect, swapping your print "<meta" with
> header("Location: Success.html");
>
> Also, if it were me, for brevity, I'd write your body line as follows:
>
> $body="Name: $name\nTel: $tel\n";
>
> This cuts down on the amount of adding strings together, which in the long
> term may or not make your code more understandable.
>
> Also, why did you comment out your validation code? If you have something
> like the below, that'd do your validation as well :)
>
> if($isValid==false) {header("Location: fail.html");} else {
> //put your mail sending code in here
> header("Location: success.html");
> }
>
> --
> Jon ""The Nice Guy"" Spriggs LPIC-1 Certified
> This message was sent from my mobile device. Please excuse any top posting
> and typos that may occur as a result.
>
> On Oct 13, 2009 4:43 PM, <Idvaughan at aol.com> wrote:
>
>  Hi
>
> I'm just starting out using PHP and have created the following contact us
> to email web form.  I am looking for some advice and best practice please
>
> Does the form processing script  look ok and secure?
>
> For example I would like to include server side validation so if a web user
> has not completed certain fields then it would display on the form page that
> they have not filled in the 'Name' field or entered a correct email address
> for example.  If so what is the best way of achieving the above with my
> script below?
>
> Also I would welcome any other best practice or advice regarding the form.
>
> Many Thanks,
>
>
> <?php
>
> // get posted data into local variables
> $EmailFrom = "idvaughan at aol.com";
> $EmailTo = "idvaughan at aol.com";
> $Subject = "Enquiry";
> $Name = Trim(stripslashes($_POST['Name']));
> $Tel = Trim(stripslashes($_POST['Tel']));
> $email = Trim(stripslashes($_POST['email']));
> $message = Trim(stripslashes($_POST['message']));
>
> // validation
> //$validationOK=true;
> //if (Trim($Name)=="") $validationOK=false;
> //if (Trim($email)=="") $validationOK=false;
> //if (Trim($message)=="") $validationOK=false;
> //if (!$validationOK) {
> //  print "<meta http-equiv=\"refresh\"
> content=\"0;URL=contactus1.html\">";
> //  exit;
> //}
>
> // prepare email body text
> $Body = "";
> $Body .= "Name: ";
> $Body .= $Name;
> $Body .= "\n";
> $Body .= "Tel: ";
> $Body .= $Tel;
> $Body .= "\n";
> $Body .= "email: ";
> $Body .= $email;
> $Body .= "\n";
> $Body .= "message: ";
> $Body .= $message;
> $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=emailok.html\">";
> }
> else{
>   print "<meta http-equiv=\"refresh\" content=\"0;URL=contactus1.html\">";
> }
> ?>
>
>
>
> _______________________________________________
> Phpwm mailing list
> Website : http://www.phpwm.org
> Twitter : http://www.twitter.com/phpwm
> Facebook: http://www.facebook.com/group.php?gid=2361609907
>
> Post to list: Phpwm at mailman.lug.org.uk
> Archive etc : https://mailman.lug.org.uk/mailman/listinfo/phpwm
>
>
> _______________________________________________
> Phpwm mailing list
> Website : http://www.phpwm.org
> Twitter : http://www.twitter.com/phpwm
> Facebook: http://www.facebook.com/group.php?gid=2361609907
>
> Post to list: Phpwm at mailman.lug.org.uk
> Archive etc : https://mailman.lug.org.uk/mailman/listinfo/phpwm
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.lug.org.uk/pipermail/phpwm/attachments/20091013/4dbd5605/attachment-0001.htm 


More information about the Phpwm mailing list