[Solved] how checking multiple field using ajax & mysql


Write a function and trigger it on blur of your email field. Pass your email as a parameter of your function and check it into your database.

This is your email field:

<input type="text" class="form-control" name="email" id="email" value="" />

This is your JavaScript code:

$(document).ready(function() {
    $("#email").blur(function(){
        var email = $("#email").val();
        if(email != ""){
            $.ajax({ 
                url: 'ajax_function_url',
                data: {email:email},
                type: 'post',
                complete: function(output) {
                    var isExist = output.responseText;
                    if(isExist === '1'){
                        alert('This email is already in use.');
                    }else{
                        alert('success!!');
                    }
                }
            });
        }else{
            alert('Email should not be empty');
        }
    });
}

This is your PHP function which is called by Ajax:

    function CheckEmailExist() {
        $conn = new mysqli($servername, $username, $password, $dbname);
        if (isset($_POST['email']) && $_POST['email'] != "") {
            $sql = "SELECT id FROM `table` WHERE `email` = '" . $_POST['email'] . "'";
            $result = $conn->query($sql);
            echo mysql_num_rows($result);
        }
        exit();
    }

This way you can check if your data exists in your database or not via ajax.

1

solved how checking multiple field using ajax & mysql