html - php inserting data with filter and validation


I have a database and I want to make sure that fields are not empty, and if not I want to save them into database with this sanitized function. So after fields are filled and sanitized I want to save them to database.

// Initialize variables to null.
$nameError ="";
$emailError ="";

          // On submitting form below function will execute.
              if (empty($_POST["name"])) {
                $nameError = "Name is required";
                } else {
                $name = test_input($_POST["name"]);
                // check name only contains letters and whitespace
                if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
                $nameError = "Only letters and white space allowed";

          if (empty($_POST["email"])) {
            $emailError = "Email is required";
            } else {
            $email = test_input($_POST["email"]);
            // check if e-mail address syntax is valid or not
            if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
            $emailError = "Invalid email format";

function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
//php code ends here

<!DOCTYPE html>
<title>Form Validation with PHP - Demo Preview</title>
<meta content="noindex, nofollow" name="robots">
<!--<link href="style.css" rel="stylesheet">-->
<div >
<div >
<div >
<h2>Form Validation with PHP.</h2>
<form action="index.php" method="post">
<span >* required field.</span><br/>
<input name="name" type="text" value=""><br/>
<span >* <?php echo $nameError;?></span><br/>
<input name="email" type="text" value=""><br/>
<span >* <?php echo $emailError;?></span><br/>

<input name="submit" type="submit" value="Submit">



html side: For checking on inputs like name and email, I suggest using the html attributerequired on your input tags. And also, you can utilize the type of input likeemail:

<input name="name" type="name" required>
<input name="email" type="email" required>

required attribute in html checks during submission of form whether you have inputted a value. If there are no values, then a warning will prompt and tell you that it is required.

an email type in input tag validates whether the user input has proper format for the email.

php side: your sanitize method and validation have no problems, but if you are looking for the best implementation regarding form validation, I suggest using a function or better, add it in a form validator class. There is a simple example here of a good validator class: Easiest Form validation library for PHP

You can make your own class based on your preferences and of course you can experience the advantages of using oop design in your code.

People are also looking for solutions to the problem: wordpress - Create a multilingual calendar in php - 'translating' with setlocale


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.