I am trying to create a function that handles the 'keyup' event for several input fields and passes the input value to a php script. Here's the code I have so far
$(document).ready(function () {
$("#email").keyup(function () {
val = $("input#email").val();
what = 'email';
aFunction(val, what);
});
});
function aFunction(val, what) {
var dataString = what + '=' + val;
var error = "email_check";
$.post("key.php", dataString, function (data) {
//if (data.[error] == 'invalid'){
if (data.email_check == 'invalid') {
$("#ppp").html('error');
} else {
$("#ppp").html('good to go');
}
}, "json");
//return false;
}
When I uncomment
//if (data.[error] == 'invalid'){
and comment out
if (data.email_check == 'invalid'){
My the script doesnt execute and js file doesn't load into the firebug script console - I assume means there's an error because when I undo that and refresh I can view it. I've tried added single and double quotes to the variable. Also, it would be helpful if there was a way to see what the is error is, but I don't know how to do that.
-
Mods - why do you keep editing my posts? Am I doing something wrong, I cant see any changes?Joe Riggs– Joe Riggs2009年12月21日 16:16:37 +00:00Commented Dec 21, 2009 at 16:16
-
I added the javascript tag to the question, as I saw the problem was just a syntax error. Is that ok?Crescent Fresh– Crescent Fresh2009年12月21日 17:07:03 +00:00Commented Dec 21, 2009 at 17:07
-
No problem at all, I just thought I was making more work for you guys :)Joe Riggs– Joe Riggs2009年12月21日 18:19:45 +00:00Commented Dec 21, 2009 at 18:19
2 Answers 2
Your primary problem here is that you should use either dot notation ("data.error") or array notation ("data['error']") but not both ("data.['error']").
Javascript does not support braces in identifiers.
If the key is actually just error, you can write if (data.error == 'invalid').
If it is [error], you'll need to write if (data['[error]'] == 'invalid)`.
To see syntax errors, go to Firebug's Console tab.