# Simple PHP server side validation



## Jacob92 (Dec 30, 2011)

Hi everyone,

I have designed contact form with following values:

First Name (Mandatory)
E-mail (Mandatory)
Phone (Optional)
Message (Mandatory)

I have validated this contact form using SpyValidation in Adobe Dreamweaver. Now, I would like to create server side validation, so it is validated if all mandatory fields are filled in, and check if any value is typed into optional text field before sending it off, and if successful next webpage called thankyou.html should appear where Thank you message appears. I have created both xhtml docs, but I'm struggling with PHP. Can anyone please help me? I have visited some links on forums, but unfortunately I'm not so good at it.


----------



## wmorri (May 29, 2008)

Hi,

First, I am interested in seeing what your form looks like so far. I can then give you some ideas for little things that I would do to add some code to your code.


----------



## DDAoN (Mar 19, 2011)

Jacob92,

Please provide the current version of your PHP code for the form, that way we can help to adapt your code to meet your needs, rather than provide you with our code to replace your own. It'll give us a better understanding of what you're actually doing with the code as that we don't hinder your efforts with a more or less restrictive solution.

I'm also curios about the visual side too, you see so many forms out there that it becomes a habit to see them all. ^^'


----------



## Jacob92 (Dec 30, 2011)

Hi guys,

do you know any good FREE web hosting so I can share my contact form with you?


----------



## DDAoN (Mar 19, 2011)

Jacob92,

There's hundreds of free hosting sources available on the Internet. Personally, I use Brinkster for my simple websites. Your best bet is to do a Google search for "free web hosting" and check out your options until you find a hoster that offers the features you want. It's been many years since I actually used free hosting, so I can't give you a suggestion on what is good now.

What I can tell you is that the best free host is your own computer. Setting up a simple apache-based server on your computer gets you a real server to test your code on and gives you the ability to put you websites out on the Internet for no more than you pay for your ISP. The catch is that until you set-up a domain you'll use your IP address to access the content.


----------



## Jacob92 (Dec 30, 2011)

Oh yeah, thats true! I didn't think of creating my own Apache server at home. I used to have Apache server for gaming website. Ill do that later on and share it with you. If we talk about DNS, what free DNS would u use? Ive tried no-ip, and dyndns, but can't get my FREE domain until I pay for it.


----------



## wmorri (May 29, 2008)

With no-ip.com you can get all the free domains you want you just have to sign up a free account with them.


----------



## Jacob92 (Dec 30, 2011)

Hi guys,

You can check out my form at Simplicity is beautiful!

This is my PHP validation, so it validates if name, email and comment was filled in, but I don't know how to make PHP code so it checks if there is a value in phone text field, and if yes - send it as well.



> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> <html xmlns="http://www.w3.org/1999/xhtml">
> <head>
> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
> ...


----------



## DDAoN (Mar 19, 2011)

Jacob92,

Impressive appearance for so simple a form. Here's my quick modification of your PHP to validate the fields when left blank. Note that the e-mail will only be processed if all three fields are filled and that even a single blank will halt the process without checking the follow on fields. Basically, if you leave both the message and the name fields blank, it will only error for the name until you fix it, then it'll error for the message. This can be changed to display all errors at once.


```
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <META HTTP-EQUIV="refresh" content="0;URL=thankyou.html">
  <title>Email Form</title>
 </head>

 <body>
  <?php
   $name=addslashes($_POST['name']);
   $email=addslashes($_POST['email']);
   $comments=addslashes($_POST['message']);

   $toemail = "[email protected]";
   $subject = "Message from my webpage";

   $headers = "MIME-Version: 1.0\n" . "From: \"" . $name . "\" <" . $email . ">\n" . "Content-type: text/html; charset=iso-8859-1\n";

   $body = "Name: " . $name . "<br>\n" . "Email: " . $email . "<br>\n" . "Comments:<br>\n" . $comments;

   if(!ereg("^[a-zA-Z0-9_][email protected][a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$", $email))
   {
    echo "That is not a valid email address. Please return to the" . " previous page and try again.";
    exit;
   }

   if($name == "")
   {
    echo "Your name was not entered into the field. Please return to the" . " previous page and try agian.";
   }
   else if($email == "")
   {
    echo "Your email was not entered into the field. Please return to the" . " previous page and try again.";
   }
   else if($comments == "")
   {
    echo "You message was not entered into the field. Please return to the" . " previous page and try again.";
   }
   else
   {
    mail($toemail, $subject, $body, $headers);
    echo "Thanks for submitting your comments";
   }
  ?>
 </body>
</html>
```
Here's a version to display all errors at the same time:

```
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <META HTTP-EQUIV="refresh" content="0;URL=thankyou.html">
  <title>Email Form</title>
 </head>

 <body>
  <?php
   $name=addslashes($_POST['name']);
   $email=addslashes($_POST['email']);
   $comments=addslashes($_POST['message']);

   $toemail = "[email protected]";
   $subject = "Message from my webpage";

   $headers = "MIME-Version: 1.0\n" . "From: \"" . $name . "\" <" . $email . ">\n" . "Content-type: text/html; charset=iso-8859-1\n";

   $body = "Name: " . $name . "<br>\n" . "Email: " . $email . "<br>\n" . "Comments:<br>\n" . $comments;

   if(!ereg("^[a-zA-Z0-9_][email protected][a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$", $email))
   {
    echo "That is not a valid email address. Please return to the" . " previous page and try again.";
    exit;
   }

   if($name == "")
   {
    echo "Your name was not entered into the field. Please return to the" . " previous page and try agian.<br>";
   }
   if($email == "")
   {
    echo "Your email was not entered into the field. Please return to the" . " previous page and try again.<br>";
   }
   if($comments == "")
   {
    echo "You message was not entered into the field. Please return to the" . " previous page and try again.";
   }
   if(($name != "") and ($email != "") and ($comments != ""))
   {
    mail($toemail, $subject, $body, $headers);
    echo "Thanks for submitting your comments";
   }
  ?>
 </body>
</html>
```
BTW, you do know that you don't have to break up the code to multiple lines, right? Eliminating the " . " connections will save you some simple errors from occurring, but it's entirely your choice. Both my versions left them intact, I just pulled them back into one line for my eyes. ^^'


----------

