Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7990787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:06:52+00:00 2026-06-04T13:06:52+00:00

I am validating an HTML Form with PHP. All of my code is fine,

  • 0

I am validating an HTML Form with PHP. All of my code is fine, except the message field. If I put something in the message field and the other fields are displaying errors, it still submits. But, if I put something into the other fields and the other fields have errors, it won’t submit, which is correct. I suspect it has something to do with the last if-else of my code. Thank you in advance.

contact.php
<?php

include 'includes/config.php';
$errors = FALSE;
$displayErrors = NULL;

if (isset($_POST['submit'])) {

$first = $_POST['first'];
$last = $_POST['last'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];

//Connect to MYSQL Database server
$connect = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("Could not connect to MYSQL Database.");
$result = mysql_select_db(DB_NAME, $connect) or die("Could not connect to MYSQL table.");

//Clean Data to prevent malicous injections
mysql_real_escape_string(strip_tags(stripcslashes(trim($first))));
mysql_real_escape_string(strip_tags(stripcslashes(trim($last))));
mysql_real_escape_string(strip_tags(stripcslashes(trim($email))));
mysql_real_escape_string(strip_tags(stripcslashes(trim($subject))));
mysql_real_escape_string(strip_tags(stripcslashes(trim($message))));

if (empty($first)) {
    $errors = TRUE;
    $displayErrors .= 'First name is invalid.<br/>';
}
if (empty($last)) {
    $errors = TRUE;
    $displayErrors .= 'Last name is invalid.<br/>';
}
if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $errors = TRUE;
    $displayErrors .= 'Email is invalid.<br/>';
}
if (empty($subject)) {
    $errors = TRUE;
    $displayErrors .= 'Subject is invalid.<br/>';
}
if (empty($message)) {
    $errors = TRUE;
    $displayErrors .= 'Message is invalid.<br/>';
} else {
    $errors = FALSE;
          //Database insertion goes here
    echo 'Form submission successful. Thank you ' . $first . '.';

}

}
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T13:06:53+00:00Added an answer on June 4, 2026 at 1:06 pm

    First off, Don’t use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi – this article will help you decide which. If you choose PDO, here is a good tutorial.


    I’ve tidied your logic, instead of using $error=true ect you should build an error array (containing your error text) and then check if thats empty at the end, I also cleaned up the magic quotes problem it seems your having, and the horrid multiple mysql_real_escape_string’s, hope it helps

    contact.php

    <?php
    include 'includes/config.php';
    //Connect to MYSQL Database server
    $connect = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("Could not connect to MYSQL Database.");
    $result = mysql_select_db(DB_NAME, $connect) or die("Could not connect to MYSQL table.");
    
    $errors = array();
    
    if ($_SERVER['REQUEST_METHOD']=='POST') {
    
        function clean(&$value){
            if (get_magic_quotes_gpc()){
                $value = mysql_real_escape_string(strip_tags(trim($value)));
            }else{
                $value = mysql_real_escape_string(trim($value));
            }
        }
        array_walk($_POST,'clean');
    
    
        if (empty($_POST['first'])) {
            $errors['first']= 'First name is invalid.<br/>';
        }else{
            $first = $_POST['first'];
        }
    
        if (empty($_POST['last'])) {
            $errors['last']= 'Last name is invalid<br/>';
        }else{
            $last = $_POST['last'];
        }
    
        if (empty($_POST['email']) || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
            $errors['email'] = 'Email is invalid.<br/>';
        }else{
            $email = $_POST['email'];
        }
    
        if (empty($_POST['subject'])) {
            $errors['subject']= 'Subject is invalid.<br/>';
        }else{
            $subject = $_POST['subject'];
        }
    
        if (empty($_POST['message'])) {
            $errors['message']= 'Message is invalid.<br/>';
        }else{
            $message = $_POST['messsage'];
        }
    
        if(empty($errors)){
            //Database insertion goes here
            echo 'Form submission successful. Thank you ' . htmlentities($first) . '.';
        }else{
            //Errors, so your have $errors['name'] ect to output
            //so somthing like:
            echo (isset($errors['name'])?$errors['name']:null);
    
        }
    
    }
    ?>
    

    You should also perhaps add a condition that the value is a min certain length, with strlen($_POST['first']) > 2 ect.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble validating an HTML form with JavaScript. On their own they each
I have a situation where validating a HTML form with jQuery Validation Plugin 1.9.0.
I am using PHP to implement an HTML form and for validation of the
I have 2 sets of 4 input fields in my HTML Form. I expect
I have an html form and some jQuery that validates the form. If all
I've written my own Code Igniter model for sending emails. All was fine until
Is there a well maintained package available in Python for creating and validating HTML
I have designed a webpage using HTML and client side validation using JavaScript.PHP for
I have an HTML form that takes inputted data and sends it via the
I have a simple HTML form that uses uses ajax in part of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.