Validation not working

index.php?
But the form action is thankyou.php.
The form action should be whatever script processes the form.

If you were to follow the principle of “separation of concerns” all the scripting and form processing would be in a controller script and the “pages” would be different html templates included depending on various conditions. Eg, first the user would see the form html, what comes next depends on the results of the form processing, if there is an error, they may be returned to the form, with some additional error message telling them where they went wrong. Only if the validation is successful, are they forwarded to the “Thank you” page.
Though this is another biggish subject and may confuse at this stage (although it should make things simpler when understood).

The (big) problem here is, the script has no output, so will appear to do nothing.
Supposing someone does not fill in their name, the string 'Please enter your name' gets assigned to the variable $name_error, but then what do you do with that variable? Nothing.
Should the error message not be echoed out somewhere?
Any script should return some result according to what happens.

If you take a look at the example code that is being used for reference, it does appear to be legacy code. Not just judging by the php, but the html and css too.
It’s all to easy for a novice to pick up bad ideas form the wrong places without being aware of it, I’ve been there. Unfortunately, I can’t think of a good form validation tutorial off hand just now.

Mittineague walked me through using action for thankyou.php to send the data to the database on another topic about sending the data to the database.

Here is a working version of your code:

<?php
error_reporting(E_ALL);

$name_error = null;
$email_error = null;
$subj_error = null;
$message_error = null;

if(isset($_POST['submit'])){
    $name = $_POST['u_name'];

    if(empty($name)){
        $name_error = 'Please enter your name';
    }
}

?>
<form action="index.php" method="post">
    <label for="u_name">Name:</label><br>
    <input type="text" name="u_name" id="u_name" ><br>
    <?php echo $name_error; ?></br>
    <label for="u_email">Email:</label><br>
    <input type="email" name="u_email" id="u_email" ><br>
    <?php echo $email_error; ?></br>
    <label for="subj">Subject:</label><br>
    <input type="text" name="subj" id="subj"><br>
    <?php echo $subj_error; ?></br>
    <label for="message">Message:</label><br>
    <textarea name="message" id="message"></textarea><br>
    <?php echo $message_error; ?></br>
    <input type="submit" name="submit" value="Submit"><br>
</form>

I know you just want to get the stupid thing working but you do need to spend some time on the basics just to understand what is going on.

1 Like

Thank you so much, now i can make a decent start on it. So to clarify the code you just displayed, does it all go into index.php?

Yes, everything is in one file. This is what @SamA74 was trying to tell you.

Eventually you may want to split it into multiple files but for now one file is fine.

Because index.php is the form action, that is where the processing will have to take place, so it’s all done in one file in this instance.
The downside to this is the user will always get returned to the form regardless of the outcome. So on successful completion, they see the form again, not the “Thank You” page. This should be enough to give you a start and get you up and running though.

But by using a controller script and splitting the html away to separate templates, you can have more control over the outcome.
This is a basic, bare-bones example to illustrate that logic.

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") { // The form is submitted
    
    // do the validation here
    
    // Then, depending on how that turns out...
    if(isset($errors)) {   // There are errors in the submission (example if condition)
        
        // Show the user the form again, with errors listed
        include 'html_form_template.php' ;
    }
    
    else {  // Everything is cool with the form submission
        
        // Send the email
        // Record to database Etc...
        
        // Show the user Thank You message
        include 'html_thankyou_template.php' ;
    }
}   // end the form is submitted

else {  // Nothing has been submitted yet
    
    // Show the user the form for the first time
    include 'html_form_template.php' ;
}

?>

Though you may not want to confuse things with that at this stage. :slight_smile:

1 Like

It’s working now thank you, can finally work on the rest of the code.

1 Like

Where have i gone wrong, getting page is not working:

<?php
error_reporting(E_ALL);

$name_error = null;
$email_error = null;
$subj_error = null;
$message_error = null;

if(isset($_POST['submit'])){
        $name = $_POST['u_name'];
        $email = $_POST['u_email'];
        $subject = $_POST['subj'];
        $message1 = $_POST['message'];

        if(empty($name)){
        $name_error = "Please enter your name";
        }
        if( !preg_match ("/^[a-zA-Z\s]+$/",$name)) {
        $name_error = "Name must only contain letters!";
        }
        if(strlen($name) <= 2){
        $name_error = "Name must contain atleast 3 letters";
        }
        if(strlen($name) >= 30{
        $name_error = "Nmae must contain less than 30 letters";
        }
        if(empty($email)){
        $email_error = "Please enter your email";
        }
        if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $email_error = "Invalid email format";
        }
        if(empty($subject)){
        $subj_error = "Please enter your subject";
        }
        if( !preg_match ("/^[a-zA-Z\s]+$/",$subject)) {
        $subj_error = "Subject must only contain letters!";
        }
        if(strlen($subject) <= 2){
        $subj_error = "Name must contain atleast 3 letters";
       if(strlen($name) >= 30{
        $name_error = "Nmae must contain less than 30 letters";
        }
        if(empty($email)){
        $email_error = "Please enter your email";
        }
        if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $email_error = "Invalid email format";
        }
        if(empty($subject)){
        $subj_error = "Please enter your subject";
        }
        if( !preg_match ("/^[a-zA-Z\s]+$/",$subject)) {
        $subj_error = "Subject must only contain letters!";
        }
        if(strlen($subject) <= 2){
        $subj_error = "Name must contain atleast 3 letters";
        }
        if(strlen($subject) >= 30{
        $subj_error = "Nmae must contain less than 30 letters";
        }
        if(empty($message1)){
        $message_error = "Please enter your message";
        }

}
?>

What does the error report say?

This is what stands out to me:-

if(strlen($name) >= 30{

A missing closing bracket.

Also you seem to be repeating some steps.
Then at some point, you need to determine if the validation was passed or not, then proceed accordingly.

the error report comes up so i am assuming it’s to do with the closing bracket.

It’s there on my code, that must of been an error copying and pasting within the nano text editor in the terminal. I’m sshing into my machine to edit the code you see :slight_smile:

Got it working again. Is there a way to not allow only special character while keeping (a-z A-Z 1-0 ?.)

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.