Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-29-2012, 04:18 PM   PM User | #1
jackvan
New Coder

 
Join Date: Jan 2012
Posts: 24
Thanks: 1
Thanked 1 Time in 1 Post
jackvan is an unknown quantity at this point
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
jackvan is offline   Reply With Quote
Old 01-29-2012, 06:41 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
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)
devnull69 is offline   Reply With Quote
Users who have thanked devnull69 for this post:
jackvan (01-30-2012)
Old 01-30-2012, 09:13 AM   PM User | #3
jackvan
New Coder

 
Join Date: Jan 2012
Posts: 24
Thanks: 1
Thanked 1 Time in 1 Post
jackvan is an unknown quantity at this point
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.
jackvan is offline   Reply With Quote
Old 01-30-2012, 09:40 AM   PM User | #4
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
.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);
devnull69 is offline   Reply With Quote
Old 01-30-2012, 11:17 AM   PM User | #5
jackvan
New Coder

 
Join Date: Jan 2012
Posts: 24
Thanks: 1
Thanked 1 Time in 1 Post
jackvan is an unknown quantity at this point
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.
jackvan is offline   Reply With Quote
Old 01-30-2012, 12:32 PM   PM User | #6
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
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
devnull69 is offline   Reply With Quote
Old 01-30-2012, 01:23 PM   PM User | #7
jackvan
New Coder

 
Join Date: Jan 2012
Posts: 24
Thanks: 1
Thanked 1 Time in 1 Post
jackvan is an unknown quantity at this point
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?
jackvan is offline   Reply With Quote
Old 01-30-2012, 01:28 PM   PM User | #8
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
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" '
devnull69 is offline   Reply With Quote
Old 01-30-2012, 01:45 PM   PM User | #9
jackvan
New Coder

 
Join Date: Jan 2012
Posts: 24
Thanks: 1
Thanked 1 Time in 1 Post
jackvan is an unknown quantity at this point
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()},
jackvan is offline   Reply With Quote
Old 01-30-2012, 01:47 PM   PM User | #10
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
Does Perl really allow for full expressions inside "" quotes?
devnull69 is offline   Reply With Quote
Old 01-30-2012, 01:53 PM   PM User | #11
jackvan
New Coder

 
Join Date: Jan 2012
Posts: 24
Thanks: 1
Thanked 1 Time in 1 Post
jackvan is an unknown quantity at this point
I don't understand what you mean by full expressions? Give me an example.
jackvan is offline   Reply With Quote
Old 01-30-2012, 01:55 PM   PM User | #12
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
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?
devnull69 is offline   Reply With Quote
Old 01-30-2012, 02:01 PM   PM User | #13
jackvan
New Coder

 
Join Date: Jan 2012
Posts: 24
Thanks: 1
Thanked 1 Time in 1 Post
jackvan is an unknown quantity at this point
Yes, like this:

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

$name is a variable with value.
jackvan is offline   Reply With Quote
Old 01-30-2012, 02:02 PM   PM User | #14
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
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?
devnull69 is offline   Reply With Quote
Old 01-30-2012, 02:03 PM   PM User | #15
jackvan
New Coder

 
Join Date: Jan 2012
Posts: 24
Thanks: 1
Thanked 1 Time in 1 Post
jackvan is an unknown quantity at this point
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",
jackvan is offline   Reply With Quote
Reply

Bookmarks

Tags
ajax, ajax jquery

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:32 PM.


Advertisement
Log in to turn off these ads.