Jquery validation plugin with remote method

Hi, I need to validate a form using data from database, I know it can be done using jquery validation plugin with remote method, in my page i’ve got a login form and the form for password recovery

This is my html page

<form class="login-form" action="includes/ctrl_login.php" method="post">
                <h3 class="form-title font-blue">Accedi alla tua area</h3>

                <div class="form-group">
                    <label class="control-label visible-ie8 visible-ie9">Nome Utente</label>
                    <input class="form-control form-control-solid placeholder-no-fix" type="text" autocomplete="off" placeholder="Username" name="username" /> </div>
                <div class="form-group">
                    <label class="control-label visible-ie8 visible-ie9">Password Utente</label>
                    <input class="form-control form-control-solid placeholder-no-fix" type="password" autocomplete="off" placeholder="Password" name="userpassword" /> </div>
                <div class="form-actions">
                    <button type="submit" class="btn blue uppercase">Accedi</button>
                    <!-- <a href="recovery.php" id="forget-password" class="forget-password">Password dimenticata?</a> -->
                </div>
                <div class="create-account">
                    <p>
                        <a href="javascript:;" id="forget-password" class="uppercase">Password Dimenticata?</a>
                    </p>
                </div>
            </form>
            <!-- END LOGIN FORM -->
            <!-- BEGIN FORGOT PASSWORD FORM -->
            <form class="forget-form" id="formforgotpassword" >
                <h3 class="font-blue">Password dimenticata?</h3>

                <p> Inserisci la tua email di seguito e ti invieremo le informazioni su come ripristinare la tua password con un semplice passaggio. </p>
                <div class="form-group">
                    <input class="form-control placeholder-no-fix" id="uemaill" type="email" autocomplete="off" placeholder="Email" name="uemaill" /> </div>
                <div class="form-actions">
                    <button type="button" id="back-btn" class="btn blue btn-outline">Login</button>
                    <button type="submit" name="submit" class="btn blue uppercase pull-right">Invia</button>
                </div>
            </form>
            <!-- END FORGOT PASSWORD FORM -->

This is the javascript

$(document).ready(function () {

            $("#formforgotpassword").validate({
                
                errorElement: 'span', 
                errorClass: 'help-block help-block-error', 
                focusInvalid: false, 
                ignore: "",

                
            rules: {
                    
                    uemaill: {
                        
                        required: true,
                        email: true,

                        remote: {
                            url: "includes/ctrl_password_recovery.php",
                            type: "post",
                            data: {
                                email: function(){
                                    return $('.forget-form :input[name="uemaill"]').val();
                                }
                            }
                        }


                    }
            },
    
            messages: {

                    uemaill: {
                        required: "Please enter your email address.",
                        email: "Please enter a valid email address.",
                        remote: "Email non esistente!"
                    }

            },
                
                highlight: function (element) { 

                $(element)
                .closest('.form-group').addClass('has-error'); 
                
                }

            });

        });


        jQuery('#forget-password').click(function() {
            jQuery('.login-form').hide();
            jQuery('.forget-form').show();
        });

        jQuery('#back-btn').click(function() {
            jQuery('.login-form').show();
            jQuery('.forget-form').hide();
        });

And this is my php file

$user_email = $_POST['uemaill'];

$query_check_user = mysqli_prepare($conn, "SELECT user_email FROM users WHERE user_email = ?");
mysqli_stmt_bind_param($query_check_user, 's', $user_email);
mysqli_stmt_execute($query_check_user);
$email_exist = mysqli_stmt_num_rows($query_check_user);
mysqli_stmt_fetch($query_check_user);

if($email_exist == $user_email){

	echo "true";

}else{

	echo "false";
}

mysqli_stmt_close($query_check_user);

Hi Vince,

Looking over your PHP code, I’ve spotted a couple of issues:

  1. You need to call mysqli_stmt_store_result($query_check_user); before checking the number of rows (see this comment on the documentation).

  2. Your IF statement is comparing an integer ($email_exist) with a string of the user’s email, which is going to return false every time. All you need to do is check that $email_exist == 1.

Set this:

$user_email = $_POST['email'];

Hi @fretburner many thanks for your answer I’ve managed to get some progress, now the only problem is that even if the email is found in the database still shows the message the email doesn’t exist and also when the email is correct it does execute the mysql insert query.

This is my javascript code:

$(document).ready(function () {

        $( "#formforgotpassword" ).validate({
            
            rules: {
                
                uemail: {
                    
                    required: true,
                    email: true,
                    remote: 'includes/ctrl_password_recovery.php'
            
                }  

            },
                 
            messages: {

                uemail: {
                        
                    required: "Ops, email address  missing!",
                    email: "Ops, email format not correct!",
                    remote: "Ops, email doesn't exist!" 
                }
            }

        });

    });

This is my php code

$user_email = $_GET['uemail'];

$query_check_user = mysqli_prepare($conn, "SELECT user_id, user_email FROM users WHERE user_email = ?");
mysqli_stmt_bind_param($query_check_user, 's', $user_email);
mysqli_stmt_execute($query_check_user);
mysqli_stmt_bind_result($query_check_user, $user_id, $useremail);

// $tot_email = mysqli_stmt_num_rows($query_check_user);
mysqli_stmt_fetch($query_check_user);
mysqli_stmt_close($query_check_user);

if($user_email == $useremail){


           echo 'true';
		$generate_token = generateRandomString();

		$stmt = mysqli_prepare($conn, "INSERT INTO user_recovery (user_id, token) VALUES (?, ?)");

		    /* bind parameters for markers */
		    mysqli_stmt_bind_param($stmt, 'ii', $user_id, $generate_token);
		    /* execute query */
		  	mysqli_stmt_execute($stmt);
		    
		    $send_mail = send_mail($user_email, $generate_token);

		    if($send_mail === 'success')
				{
						echo 'Good, we sent you an email to recovery your password';
						

				}else{
					
					echo 'Ops, problem';
				
				}
	

}else{

	echo 'false';
}

Hi Vince,

I’m a bit confused about what you’re doing here. Are you trying to send a password recovery email as part of the form validation check?

Hi @fretburner yes I was trying to do both check and password recovery form the same file, but I realized it’s not possible to I’ve created two separate files, one to manage the validation and another one to manage the form request ad it seems to work fine. Many thanks for your help :wink:

1 Like

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