Ajax email Availability check with php not working

前端 未结 2 1339
隐瞒了意图╮
隐瞒了意图╮ 2021-01-07 05:23

I want to check email availability but it\'s not working. and also I am new to javascript and ajax please help me.

his is my code email input with span to show outpu

2条回答
  •  孤城傲影
    2021-01-07 06:08

    You should check link I refered in comment its your complete answer.

    here is a Simple example with your code.

    include("DbConn.php");
        // Set alerts as array 
    $error     = "";
    
        // I should just trrim and let you check if email is empty .lol 
    if (empty($_POST["email_val"])) {
        $error .= "

    Fill email value.

    "; //Check if this is a real email } elseif(!filter_var($_POST["email_val"],FILTER_VALIDATE_EMAIL)){ $error .= "

    Wrong email type.

    "; }else{ $email = mysqli_real_escape_string($conn, $_POST["email_val"]); //You should use prepare statement $email, Shame on you .lol $query = "SELECT * FROM customer WHERE email = '{$email}'"); $result = mysqli_query($conn, $query); echo mysqli_num_rows($result); $error .= "ok"; } $data = array( 'error' => $error );

    This Jquery :

     $(document).ready(function(){
        $('#myform').submit(function(event){
        event.preventDefault();
        var formValues = $(this).serialize();
            $.ajax({
            url:"includes\emailAvailability.php",
            method:"POST",
            data:formValues,
            dataType:"JSON",
                success:function(data){
                    if(data.error === 'ok'){
                        $('#result').html(data.error);
                    } else {
                        $('#result').html(data.error);
                        $('#myform')[0].reset();
                    }
                }
            });
        });
    });
    

    And Html :

提交回复
热议问题