数据未插入数据库 - PHP MYSQL

Trying to get together the sign up validation with PHP and Ajax. Not sure what is wrong but the submission does not happen. If I don't add the validation part everything looks fine and i am able to insert the data to mysql.

<script type="application/javascript">

    $("#submit").submit(function (event) {
        event.preventDefault();
        var datatopost = $(this).serializeArray();
        console.log(datatopost);
        $.ajax({
            url: "signupregister.php",
            type: "POST",
            data: datatopost,
            success: function (data) {
                if (data) {
                    $("#signupmessage".html(data));
                }
            },
            error: function () {
                $("#signupmessage").html("<div class = 'alert alert-danger'></div>")
            }
        });
    });
</script>

_

<?php    

session_start();
include('mysqlconnection.php');
include('index.php');

function customError($errors, $errorslevel)
{
}

set_error_handler("customError", E_ALL);

if (isset($_POST['submit'])) {
    if ($_POST($first_name) == "") {
        $errors .= $first_nameError;
    } else {
        $first_name =
            filter_var($_POST["first_name"], FILTER_SANITIZE_STRING);
    }
}


if ($errors) {
    $resultMessage = '<div class="alert alert-danger">' .
        $errors . '</div>';
    echo $resultMessage;
    exit;
}
$first_nameError = '<p>first name required</p>';

First up, in your validation PHP script, you won't need to include 'index.php'

Try redirecting the form to an empty page where you only include your validation data, while setting a session variable for the first error encountered.

At the end of your validation, if your error variable contains an error, you can redirect to the form and display your said error at a convenient location. Keep in mind you will have to save form data in session variables if you want to preserve all user input (thus removing the hassle of refilling the form over and over again). If it doesn't, you can proceed to insert the data in your db then redirect to your desired landing page.

Here's a sample code based on your input:

<?php
  session_start();
  if(isset($_POST['submit'])){
        $_SESSION['fname'] = $_POST['fname'];
        //so on for your other variables
        if($_SESSION['fname'] == ""){
            $_SESSION['err'] = "First Name Required";
        }
        if(//insert your format validation for first name){
             $_SESSION['err'] = "First Name Invalid";}
        }
        //end of validation
        if isset($_SESSION['err']){
           header('Location: myform.php');
        }
        else{
            //save all your variables into normal ones, i.e $fname-$_POST['fname'];
           //insert into database;
           //check correct insertion;
           //redirect to landing page;
}
}
?>