![]() |
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:
username_check_script_url?? The followings is a test script to illustrate the problem: PHP Code:
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 |
Obviously you don't know the difference between the string "data" and the variable data in those two statements
Code:
alert("data"); |
Jquery Replace
Thanks, it works now!
Now I want to do replace(), Here's what I did: PHP Code:
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. |
.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"); |
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:
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. |
Either this
Code:
url:"/cgi-bin/isaccount.cgi?username=" + encodeURIComponent(name.val()),Code:
url:"/cgi-bin/isaccount.cgi", |
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? |
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' " |
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()}, |
Does Perl really allow for full expressions inside "" quotes?
|
I don't understand what you mean by full expressions? Give me an example.
|
This
Code:
url: "/cgi-bin/isaccount.cgi?username=name.val()", |
Yes, like this:
url: "/cgi-bin/isaccount.cgi?username=$name", $name is a variable with value. |
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? |
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.