0

Hello, I want to ask, what going on for this. I try to implement form, user key in the username, it will trigger the function to check the username is existing or not.

I tried to figure out the problem, but dont know what's going on...

My Js file,

jQuery(document).ready(function() {
    $('.register form').submit(function(){
        $(this).find("label[for='username']").html('Username');

        var username = $(this).find('input#user_name').val();


        if(username != ''){
            $.ajax({
                url: 'checkuser.php', 
                type: 'POST',
                dataType: 'json', 
                data: {user_name: username},

                beforeSend: function(){
                    console.log(username);
                },

                success: function (data) {
                    console.log(data);
                    if(data.success==true){
                        $(this).find("label[for='username']").append("<span style='display:none;  font-size:12px;' class='red'> - ok</span>");
                        $(this).find("label[for='username'] span").fadeIn('medium');
                        return false;
                    }
                    else{
                        $(this).find("label[for='username']").append("<span style='display:none;  font-size:12px;' class='red'> - failed</span>");
                        $(this).find("label[for='username'] span").fadeIn('medium');
                        return false;
                    }
                }
                }).fail(function() {
                    $(this).find("label[for='username']").append("<span style='display:none;  font-size:12px;' class='red'> - error</span>");
                    $(this).find("label[for='username'] span").fadeIn('medium');
                    return false;
                });


        }


    });


});
}

my CheckUser.php

<?php
    error_reporting(0);             
    include 'dbc.php';

    foreach($_GET as $key => $value) {
        $get[$key] = filter($value);
    }

    foreach($_POST as $key => $value) {
        $data[$key] = filter($value);
    }

    $user = $data['user_name']; 


    $rs_duplicate = mysqli_query($link, "select count(*) as total from users where user_name='$user' ");

    if(!$rs_duplicate)
        echo("Error description: " . mysqli_error($link));

    else{
        list($total) = mysqli_fetch_row($rs_duplicate);

        if($total > 0) {
            $return['success'] = false;
            $return['msg'] = "Username already exists.";
            //echo '<span class="error" style="font-size:14px;">Username already exists.</span>';
            //exit;
        }

        else if(strlen($user) == 0){
            $return['success'] = false;
            $return['msg'] = "exxsxx";
            //echo '<span class="error"></span>';
        }

        else {
            $return['success'] = false;
            $return['msg'] = "Use alphanumeric characters only.";
              //echo '<span class="error" style="font-size:14px;">Use alphanumeric characters only.</span>';
        }
    }
    echo json_encode($return);
?>

Edited by Lau_1

2
Contributors
4
Replies
33
Views
2 Years
Discussion Span
Last Post by diafol
0

We need your markup too. But you're probably submitting the form normally.

Place a prevent default event in the submit handler;

I'd suggest NOT placing inline styling in the injected markup - use a CSS class.

Not sure why you're using $_GET and $_POST in your php script. It seems you're sending via POST in your Ajax. If you're not changing anything on the serever - just checking - then use GET (not for passwords though!).

A problem with your question is that you give absolutely no info about the nature of the problem. Thread title gives no insight. What does the console show? Is success or fail running at all? You need to provide much more info.

0

Thanks for your reply, i will change that soon. But before changing, i need to make it work.

Console didnt display anything. Never mind, i tried other method, and this method might be poor. But I want to know why this doesnt work

$.post("checkemail.php", {usr_email: email} , function(data){           
            if (data != '' || data != undefined || data != null) {                 
                    $(this).find("label[for='email']").append("<span style='display:none;  font-size:12px;' class='red'> - Email already exist.</span>");
                    $(this).find("label[for='email'] span").fadeIn('medium');
                    return false;
                }

            });

I am sure that checkmail.php will return the value. But the Form will not go through this function, but this work,

$.post("checkemail.php", {usr_email: email} , function(data){           
            if (data != '' || data != undefined || data != null) {                 
                    localStorage.setItem("email_status" , data);
                }
                else
                    localStorage.setItem("email_status" ,"No");
            });

            if(localStorage.getItem("email_status") == 1)
            {
                $(this).find("label[for='email']").append("<span style='display:none;  font-size:12px;' class='red'> - Email already exist.</span>");
                $(this).find("label[for='email'] span").fadeIn('medium');
                return false;
            }

Any way to improve this?

Edited by Lau_1

0

And may i know why

$.post("checkemail.php", {usr_email: email} , function(data){           
            if (data != '' || data != undefined || data != null) {                 
                    localStorage.setItem("email_status" , data);
                }
                else
                    localStorage.setItem("email_status" ,"No");
            });

// why go to this 1st, then back to $.post?

            if(localStorage.getItem("email_status") == 1)
            {
                $(this).find("label[for='email']").append("<span style='display:none;  font-size:12px;' class='red'> - Email already exist.</span>");
                $(this).find("label[for='email'] span").fadeIn('medium');
                return false;
            }

Edited by Lau_1

0

But before changing, i need to make it work.

If you don't change anything when it doesn't work, don't expect it to suddenly work. I'm not sure I understood.

You seem to be presenting new bits of code - were these used previously? You still haven't show your markup.

This is getting frustrating. Another thread where your markup or code seem to be a secret and we have to drag it out of you. localstorage wasn't even mentioned originally. OK, good luck with it. Anybody else?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.