CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Ajax and Design (http://www.codingforums.com/forumdisplay.php?f=55)
-   -   Not getting any data from Ajax success response (http://www.codingforums.com/showthread.php?t=250126)

jackvan 01-29-2012 04:18 PM

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

devnull69 01-29-2012 06:41 PM

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)

jackvan 01-30-2012 09:13 AM

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.

devnull69 01-30-2012 09:40 AM

.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);


jackvan 01-30-2012 11:17 AM

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.

devnull69 01-30-2012 12:32 PM

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

jackvan 01-30-2012 01:23 PM

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?

devnull69 01-30-2012 01:28 PM

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" '


jackvan 01-30-2012 01:45 PM

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()},

devnull69 01-30-2012 01:47 PM

Does Perl really allow for full expressions inside "" quotes?

jackvan 01-30-2012 01:53 PM

I don't understand what you mean by full expressions? Give me an example.

devnull69 01-30-2012 01:55 PM

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?

jackvan 01-30-2012 02:01 PM

Yes, like this:

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

$name is a variable with value.

devnull69 01-30-2012 02:02 PM

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?

jackvan 01-30-2012 02:03 PM

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",


All times are GMT +1. The time now is 01:19 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.