Jump to content

Recommended Posts

Hi guys,

 

I'm sure this is pretty straight forward, but I have very limited PHP experience. Can someone please let me know the best way to add validation to the following contact form?:

<?php

$EmailTo = "email@domain.com";
$Subject = "Website Contact Page";
$EmailFrom = Trim(stripslashes($_POST['Name']));

$Name = Trim(stripslashes($_POST['Name']));
$Email = Trim(stripslashes($_POST['Email']));
$Website = Trim(stripslashes($_POST['Website']));
$Message = Trim(stripslashes($_POST['Message']));

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

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Telephone: ";
$Body .= $Telephone;
$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=thanks.html\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
 

 

Any help greatly appreciated. Thanks in advance.

 

 

Link to comment
https://forums.phpfreaks.com/topic/275051-contact-form-validation/
Share on other sites

Is it the valid email address you want? Someone has put a lot of work into doing this. I can dig it out if you want.

 

Hi Otuatail. Thanks for your reply.

 

The basic requirement at present is simply to check whether there's an entry for each field at all, before sending the message, rather than checking a given address itself exists.

 

Any help you can offer would be great though.

 

Sam

If you want to validate the e-mail, then filter_var with the FILTER_VALIDATE_EMAIL flag is all you need.

You should also remove all of those stripslashes, as they are not needed (and can be quite detrimental to the security). If you have magic_quotes activated, turn them off instead!

 

As for validation: That's something you do when you are retrieving the data from the user, in this instance the $_POST array. How to validate the values depends entirely upon what those values are meant to contain, and what you expect within those parameters. Names validate completely differently to numbers, which validate completely differently to phone numbers, and so forth.

 

Validation is short way of saying: To check the input against a set of rules which confirms its adherence to the expected pattern, for that type of input, as determined by your business rules and requirements.

That's why I recommend that you search around a bit on this forum, for "{type} validation" where "{type}" is what you're looking to validate. Such as "name", "email" and so forth. You should find plenty of good information when doing so, both from me and from other people.

I know there's a great tutorial on input validation too, but I can't find it at the moment.

 

When the validation fails, and it will do, that's a good time to store an error message so that you can tell the user. Once you've validated all fields, echo the collected messages to the user and show the form anew. Giving him a chance to correct his/her mistakes, and then re-submit the form.

Once everything validated, and only then, you can save them to the database, mail them to yourself, or do whatever you like.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.