0

i have a php code to check if a user already exists in my db , but i would like it to go through jquery first .

This way instead of getting an error message on a new page , the user simply gets a message that the user already exists and can change the name.

Looks like this :

form

PHP CODE:

<?php

$link = mysqli_connect("localhost","root","","data1") or die("Error " . mysqli_error($link));
$usercheck = "csdcsdsdf";

$sanitizeduser= filter_var($usercheck, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);

$result = $link->query("SELECT username FROM users WHERE username = '".$sanitizeduser."' ");

$row_cnt = $result->num_rows;    
if($row_cnt>0){     
    echo "User Exists";
}else{      
    echo "No User found";
}

?>

I'm using a predefined jquery code (this script : http://jqueryvalidation.org/)'

part of my jquery code to validate names :

names: function(value, element) {
            return this.optional(element) || /^\w+$/.test(value);
        },

Full Jquery code :

file 1 : http://jsfiddle.net/EjSbd/1/ (script.js)

file 2 : http://jsfiddle.net/qM4Uz/1/ (jquery.validate.js)

5
  • "data1 " is your password ? Commented Feb 5, 2014 at 17:56
  • mysqli_connect(host,username,password,dbname,port,socket); take this parameters ; Commented Feb 5, 2014 at 18:01
  • normally i used $db_server=mysql_connect($db_hostName,$db_userName,$db_password); for connect with my server if you want i can provide you my source code as sample for guide line ..if you want ? Commented Feb 5, 2014 at 18:08
  • @code360 - yep sure , that would be great Commented Feb 5, 2014 at 18:59
  • give me ur mail address John_Nil Commented Feb 5, 2014 at 19:05

3 Answers 3

1

If you are using jQuery validation plugin then here is the solution. This is active code. you need to use remote: . This is my existing code. This will help you to get idea.

$("form#form-join-1").validate({
        rules: {
            sponsorID: {
                required: true,
                minlength: 5,
                remote: "ajax.php"
            },
            slcchild: {
                required : true,
                remote : {
                    url: "ajax.php",
                    type: "post",
                    data: {
                        action : 'child-validation',
                        parent_node : function(){
                            return $('input#sponsorID').val();
                        }
                    }
                }
            }
        },
        messages: {
            sponsorID:  {
                required: "Enter a Sponsor ID",
                minlength: $.format("Enter at least {0} characters"),
                remote: $.format("{0} is already in use")
            },
            slcchild:  {
                required: "Select A node",
                remote: $.format("{0} is not empty")
            }
        }
    });

Here is complete guideline. Following this link. They have example code. http://imamiscool.wordpress.com/2009/06/29/check-email-availability-using-jquery%E2%80%99s-ajax-part-2-easy-way/

Sign up to request clarification or add additional context in comments.

3 Comments

- any idea how to make it work with my code posted above ? - I'm not really sure how to merge the code
Yes. I have. i will send you more details. when i get back to home. i am outside now.
Please Check attached Link. They have Complete example. Download their source code.
0

Try this, You need to add remote: "usernamecheck.php" Assumed your php file name is usernamecheck.php

username: {
  minlength: 5,
  maxlength:20,
  required: true,
  wordonly: true,
  remote: "usernamecheck.php"
},

Ref: http://jqueryvalidation.org/category/methods/

Comments

0

One of the easier ways is to use one of jQuery's Ajax functions, .load()

So first you make an HTML div where you want your dynamically generated content to be:

<div id="area_to_load_at">
    This text will be replaced when JS runs changeArea()
</div>

Next you make a Javascript function that's just for changing the div:

function changeArea()
    {
    var loadTo = "#area_to_load_at";
    var loadFrom = "your_file_that_checks_if_the_username_is_taken.php";

    $(loadTo).load(loadFrom); 
    }

Then anywhere in the Javascript where you want it, like right after your validation, just call:

changeArea();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.