Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Simple PHP server side validation

Featured Replies

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.


if( isset($_POST) ){

//form validation vars
$formok = true;
$errors = array();


//form data
$name = $_POST['name'];	
$email = $_POST['email'];
$phone= $_POST['phone'];
$message = $_POST['message'];

//validate form data

//validate name is not empty
if(empty($name)){
	$formok = false;
	$errors[] = "You didn't put in a name";
}

if(empty($phone)){
	$formok = false;
	$errors[] = "U didn't fill in a number";	
}

//validate email address is not empty
if(empty($email)){
	$formok = false;
	$errors[] = "U heeft geen emailadres ingevult";
//validate email address is valid
}elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){
	$formok = false;
	$errors[] = "You didn't put in an emailaddress";
}

//validate message is not empty
if(empty($message)){
	$formok = false;
	$errors[] = "You didn't put in a message";
}
//validate message is greater than 15 charcters
elseif(strlen($message) < 15){
	$formok = false;
	$errors[] = "Your message must be longer then 15 characters";
}

 

This is just the code for validation, not for sending the email!

 

If you need help with that just ask :)

Edited by RowruffDesign

  • Author

Thanks, is that PHP? I have tried to paste that into PHP document into Adobe Dreamweaver but it is not detecting its PHP.

 

Do you also know how to create PHP for sending ?

Thanks, is that PHP? I have tried to paste that into PHP document into Adobe Dreamweaver but it is not detecting its PHP.

 

Do you also know how to create PHP for sending ?

 

You need to add the PHP opening and closing tags

  • Author

<?php
if( isset($_POST) ){

       //form validation vars
       $formok = true;
       $errors = array();


       //form data
       $name = $_POST['name']; 
       $email = $_POST['email'];
       $phone= $_POST['phone'];
       $message = $_POST['message'];

       //validate form data

       //validate name is not empty
       if(empty($name)){
               $formok = false;
               $errors[] = "You didn't put in a name";
       }

       if(empty($phone)){
               $formok = false;
               $errors[] = "U didn't fill in a number";        
       }

       //validate email address is not empty
       if(empty($email)){
               $formok = false;
               $errors[] = "U heeft geen emailadres ingevult";
       //validate email address is valid
       }elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){
               $formok = false;
               $errors[] = "You didn't put in an emailaddress";
       }

       //validate message is not empty
       if(empty($message)){
               $formok = false;
               $errors[] = "You didn't put in a message";
       }
       //validate message is greater than 15 charcters
       elseif(strlen($message) < 15){
               $formok = false;
               $errors[] = "Your message must be longer then 15 characters";
       }
?>

 

 

Dreamweaver tells me syntax error on line 48, which is ?>

What is wrong? I just opened and closed php tags.

 

 

 

Dreamweaver tells me syntax error on line 48, which is ?>

What is wrong? I just opened and closed php tags.

 

There is a curly brace missing from the end of the code, should be:

 

}

}

?>

Edited by GalaxyTramp

  • Author

I see, thanks.

 

Do you know how to make this code sending e-mail, and once form is sent, show next "thank you" web page?

I want to achieve this:

 

Have my form validated on client side by Java, then once clicked on submit, validated by PHP on server side, and then if sucessful sent form and show client next web-page saying thank you etc.

Remove the ?> from the end of your PHP file, and put this in (and change the settings at the top):

 

$to 		= "Your Name <you@youremail.com>";
$subject 	= "Contact form submission";
$success_page 	= "/contact_success.php";

if (empty($errors)){
$body = "Contact form submission:\n\n";
foreach ($_POST as $field => $val){
	$body .= ucfirst($field) . ": $val\n";
}
$body .= "\nSent at " . date("H:i, jS F");
if (mail($to, $subject, $body)){
	header("Location: $success_page");
	exit();
} else {
	$errors[] = "Email could not be sent!";
}
}

?>

<h1>Sorry, an error occurred</h1>
<ul>
<? foreach ($errors as $error) :?>
<li><?= $error ?></li>
       <? endforeach ?>
</ul>

Edited by andyl

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.