views:

61

answers:

2

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.

+2  A: 

Your primary problem here is that you should use either dot notation ("data.error") or array notation ("data['error']") but not both ("data.['error']").

JacobM
It's actually called "bracket notation" ;)
kangax
data[error] is what I needed
jriggs
ps - what's my secondary problem?
jriggs
+1  A: 

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.

SLaks
+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
jriggs