0

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.

Colin Brock
21.6k9 gold badges51 silver badges62 bronze badges
asked Dec 21, 2009 at 16:01
3
  • Mods - why do you keep editing my posts? Am I doing something wrong, I cant see any changes? Commented 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? Commented Dec 21, 2009 at 17:07
  • No problem at all, I just thought I was making more work for you guys :) Commented Dec 21, 2009 at 18:19

2 Answers 2

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']").

answered Dec 21, 2009 at 16:07
Sign up to request clarification or add additional context in comments.

2 Comments

It's actually called "bracket notation" ;)
ps - what's my secondary problem?
1

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.

answered Dec 21, 2009 at 16:08

1 Comment

+1 thanks for the heads up on console tab. I kept googling for 'validate jquery code' and it came back with links for the validator plugin

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.