Can't connect to database inside file triggered using AJAX : I'm using AJAX to trigger my validation.php file. I want to connect to my database in that file, so I included connect.php file but it's causing some problem.

    <script type="text/javascript">
    $(document).ready(function(){
        $("#offer-form").submit(function(e){
            e.preventDefault();

            $.ajax({
                type : 'POST',
                data: {
                    'name': $('#name-input').val()
                },
                url : 'validation.php',
                success : function(data) {
                    $("#name-error").text(data["name-error"]);
                }
            });
            return false;
        });
    })
</script>

validation.php

    <?php
    header('Content-Type: application/json');

    $error = array("name-error" => "");

    require_once "connect.php";

    $error['name-error'] = "error";

    echo json_encode($error);
    ?>

connect.php

    <?php 
    $serverName = "localhost";
    $dbName = "test";
    $username = "root";
    $password = "";

    try 
    {
        $conn = new PDO("mysql:host=$serverName; dbname=$dbName",
                       $username,
                       $password, 
                       array(PDO::ATTR_EMULATE_PREPARES => false,
                             PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
                       );
        echo "Connected successfully"; 
    }
    catch (PDOException $e) 
    {
        echo "Connection failed: " . $e->getMessage();
    }
    ?>

I simplified my code. The thing is that using *require_once* inside my validation.php file causes *name-error* no to be displayed. Another strange thing to me is that this file works when I type .../validation.php in my browser. I'm already using connect.php in my main file (one including script shown above) and it works fine.
