2

When i use blur function on textbox to check duplicate name using jquery ajax working fine.

Here is code:

function duplicate(data){
    //alert(data); //successfully gives the value from the text input
    $.post("test.php", {name: data}, function (data){
        if(data){
            alert('duplicate name');
        }
    });
}

$(function() {
    $("#name").blur(function(){
        var data = $("#name").val();
        duplicate(data);
    });
});

The problem is it show alert message, in some case we submit with duplicate name itself so there is no use of checking duplication.

Let me know the solution to check duplicate name using onsubmit function ( jquery ajax or javascript ).

2
  • what exactly is test.php returning? Commented Jul 22, 2009 at 12:24
  • Here the test.php: $name = $_POST['name']; if( $name == 'test'){ echo "Duplicate Name"; }else{ echo "Not a duplicate"; } If its not a duplicate then i save the name into record using php code. Commented Jul 24, 2009 at 5:41

2 Answers 2

3

Your question is confusing because you're using the same variable name, data, outside of your request as well as in the request callback.

I'm not sure if you're asking to check for duplication based on data that has already been submitted and is back on the page, data that exists on the server, or if the data's simply "yes".

Based on the code you provided, it would appear that data is considered to be duplicated if the server returns yes once the POST completes.

Either way, maybe this will help:

$(function() {
  $('#name').blur(function() {
    duplicate($('#name').val());
  });
});

function duplicate(sData) {

  var sDuplicateValue = ...; // assign this whatever constitutes a duplicate value
  if(isDataDuplicate(sData, sDuplicateValue)) {
     // you have duplicate data
  }

  $.post('test.php', { name: sData }, function(sResposeData) {

    /* because of question ambiguity, i don't know if you want to compare
     * sData and sResponseData, or sResponseData and "yes." if it's the latter,
     * just do isDataDuplicate(sResponseData, "yes"); otherwise, do this:
     */

    if(isDataDuplicate(sData, sResponseData) {
      // it's the same..
    }
  });

}

function isDataDuplicate(sData, sDuplicateValue) {
  if(sDuplicateValue === null) {
    return sData === 'yes';
  } else {
    return sData === sDuplicateValue;
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1

I'll do something like this:

$(function() {
    $("#name").blur(function(){
        var value = $("#name").val();
        $.post(
            "checkDuplicates.php",
            { name: value}, 
            function (data){
                if( data.response === 'yes'){
                    $("#name").css({
                        'border': '1px red solid'
                    }).parent().append('This name already exists');
                } else {
                    return false;
                }
            },
            'json'
        );
    });
});

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.