Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    New Coder
    Join Date
    Jan 2012
    Posts
    24
    Thanks
    1
    Thanked 1 Time in 1 Post

    Not getting any data from Ajax success response

    I'm learning how to do Ajax with Jquery to validate a Form.

    First it checks the format of the username to see if it is an valid format, if the format is correct than use Jquery Ajax to check a script to see if the username is available or not.

    Here are the Ajax portion of the code:

    PHP Code:

    $.ajax({
        
    url:"username_check_script_url",
        
    async:false,
        
    success:function(data){
            
    alert("data");
            return 
    true;
            }
    }); 
    When the above Script was run, it can get into the success:function(data) loop, but it will not return the data from the
    username_check_script_url??

    The followings is a test script to illustrate the problem:

    PHP Code:
        function validateName(){
            
    //if it's NOT valid
            
    if(name.val().length 4){
                
    alert("We want names with more than 3 letters!");
                return 
    false;
            }
            
    //if it's valid, check name available or not
            
    else{
                
                    $.
    ajax({
                    
    url:"ajax.txt",
                    
    async:false,
                    
    success:function(data){
                        
    alert("data");
                        return 
    true;
                        }
                });
                return 
    true;
            }
        } 
    "ajax.txt" has the words: "OK to use"

    The intention of the script is if I enter a name with more than 3 letters, it will fetch "ajax.txt", then return the data in "ajax.txt" which is "OK to use" and shows in the alert message.

    But when I enter "abcd" in the Form, I get a pop-up warning message: "data" instead of "OK to use"??

    It appears that Ajax communicated with "ajax.txt", but when it gets into the success:function(data){ loop, it is not returning the data from the "ajax.txt"?

    Kindly help to explain what I did wrong and how to fix the problem?

    Thanks

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    Obviously you don't know the difference between the string "data" and the variable data in those two statements

    Code:
    alert("data");
    
    alert(data);
    The first line outputs the string literal "data" and the second one outputs the content of the variable data (which is what you wanted to achieve)

  • Users who have thanked devnull69 for this post:

    jackvan (01-30-2012)

  • #3
    New Coder
    Join Date
    Jan 2012
    Posts
    24
    Thanks
    1
    Thanked 1 Time in 1 Post

    Jquery Replace

    Thanks, it works now!

    Now I want to do replace(),

    Here's what I did:

    PHP Code:
                 $.ajax({ 
                    
    url:"ajax.txt"
                    
    async:false
                    
    success:function(data){ 
                        
    data.replace(/yes/, "green");
                        
    alert("data"); 
                        return 
    true
                        } 
    ajax.txt has the word "yes" .

    The intention is to replace the word "yes" with the word "green" using this:
    data.replace(/yes/, "green");

    But it does not work because the pop-up warning still shows: "yes". It should be "green"??

    Kindly advise what I did wrong?

    Thanks.

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    .replace() will RETURN the new value it will not CHANGE the current value. So you'll have to do an assignment of the return value like this
    Code:
    data = data.replace(/yes/, "green");
    alert(data);

  • #5
    New Coder
    Join Date
    Jan 2012
    Posts
    24
    Thanks
    1
    Thanked 1 Time in 1 Post

    How to send data value to a script

    Thanks, it works!

    I want to send username data entered in the Form to a script which will then check the database for username availability. I've trouble getting it work.

    Here's what I did:
    PHP Code:
    var name = $("#name");
    $.
    ajax({
        
    url:"/cgi-bin/isaccount.cgi?name.val()",
        
    async:false,
        
    success:function(data){

            if(
    data=="no"){
                
    alert("name ok");

            }
            if(
    data=="yes") {
                
    alert("Name In Use");
            }
            return 
    true;
            }

    The intention is to send the value of name to the script, isaccount.cgi for it to check username availability. It does not work because the value of name.val() is not passed to the script isaccount.cgi.

    I don't think removing the " " in the following line will work:
    url:"/cgi-bin/isaccount.cgi?name.val()",

    Kindly advise how to do it.

    Thanks.

  • #6
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    Either this
    Code:
    url:"/cgi-bin/isaccount.cgi?username=" + encodeURIComponent(name.val()),
    or (better and easier) this
    Code:
    url:"/cgi-bin/isaccount.cgi", 
    data : {username: name.val()},
    You should get into some javascript (or coding) basics ... like string concatenation, data types etc

  • #7
    New Coder
    Join Date
    Jan 2012
    Posts
    24
    Thanks
    1
    Thanked 1 Time in 1 Post
    Thanks again.

    You are correct. I should do more read-ups on Javascript basic coding knowledge.

    I'm used to doing Perl Scripts. I thought some of the codes are similar, but they are not. That's why it's causing problems for me now.

    As examples:

    In Perl: variables inside " " are treated as variables, but not in Javascript, they are treated as strings.

    In Perl: variables inside ' ' are treated as strings, so is Javascript.

    So what's the differences for the use of " " and ' ' in Javascript?

  • #8
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    There is no special difference. You can use both (as long as you use them in pairs) for strings. It makes life easier if you want to use quotes inside string literals, as you can just use the other one as the string delimiter
    Code:
    "This is a 'string' "
    'This is a "string" '

  • #9
    New Coder
    Join Date
    Jan 2012
    Posts
    24
    Thanks
    1
    Thanked 1 Time in 1 Post
    No differences...Hmm..then I think the Perl code inventer has a better idea in the use of " " and ' '.

    If Javascript inventers will do the same like Perl, treat " " and ' ' differently, i.e. variables inside " " are treated as variables.

    Then my problem earlier can be solved easily like this:

    url: "/cgi-bin/isaccount.cgi?username=name.val()",

    Instead of this:

    url:"/cgi-bin/isaccount.cgi?username=" + encodeURIComponent(name.val()),

    Or this:

    url:"/cgi-bin/isaccount.cgi",
    data : {username: name.val()},

  • #10
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    Does Perl really allow for full expressions inside "" quotes?

  • #11
    New Coder
    Join Date
    Jan 2012
    Posts
    24
    Thanks
    1
    Thanked 1 Time in 1 Post
    I don't understand what you mean by full expressions? Give me an example.

  • #12
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    This
    Code:
    url: "/cgi-bin/isaccount.cgi?username=name.val()",
    would not just be using the variable "name" but it would also incorporate the execution of the method val() of the variable. Is something like this possible in Perl?

  • #13
    New Coder
    Join Date
    Jan 2012
    Posts
    24
    Thanks
    1
    Thanked 1 Time in 1 Post
    Yes, like this:

    url: "/cgi-bin/isaccount.cgi?username=$name",

    $name is a variable with value.

  • #14
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    This is different

    The variable in Javascript is "name" and in Perl it's "$name". I get that. But you thought that also name.val() was possible inside "" quotes which is the result of the execution of a method and not only a variable. Is something like this possible in Perl?

  • #15
    New Coder
    Join Date
    Jan 2012
    Posts
    24
    Thanks
    1
    Thanked 1 Time in 1 Post
    In Perl, we do this:

    $name = 'jack';

    Then:

    url: "/cgi-bin/isaccount.cgi?username=$name",

    It's exactly like this:

    url: "/cgi-bin/isaccount.cgi?username=jack",


  •  
    Page 1 of 2 12 LastLast

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •