1

I used to create an array in js

var data = new Array();  
data['id'] = self.iframeFields.id.val();  
data['name'] = self.iframeFields.name.val();  
data['location'] = self.iframeFields.location.val();  
data['about'] = self.iframeFields.about.val();  
data['company'] = self.iframeFields.company.val();  
data['website'] = self.iframeFields.website.val();

but passing var data returns null value

but data['id'] return value.

What did I do wrong?

EDIT: After nrabinowitz's answer, i was using

if ($.isArray( data )){ ajax({

        url: myurl,
        data: {
            method: "updateProfile",
            data: data
        },
        normalizeJSON: true,
        success: function( response ){
            // Check to see if the request was successful.
            if (response.success){
                alert(response);
            } else if (onError){
                // The call was not successful - call the error function.
                alert(response);
            }
        }
    });
       }

as it is an object not an array,

it was not returning nothing,

Removing

  if ($.isArray( data )){
        }

solves the issue.

5
  • "passing var data returns null value": What does that mean? And why do you use an Array, when the data is keyed by name (not index)? Commented Jan 12, 2012 at 6:22
  • what self.iframeFields holds?? Commented Jan 12, 2012 at 6:23
  • @diEcho it holds iframe contents Commented Jan 12, 2012 at 6:25
  • @Thilo to loop an array in php and tag will the database field Commented Jan 12, 2012 at 6:26
  • 1
    You should google this as well, "dbug js using firebug", Commented Jan 12, 2012 at 6:31

2 Answers 2

3

In Javascript, you want an object, not an array:

var data = {};
data['id'] = self.iframeFields.id.val();
// etc...

You're expecting the array to work like an associative array in PHP, but Javascript arrays don't work that way. I'm assuming you're setting these values by key, and then trying to iterate through the array with something like a for loop - but while you can set values by key, because in Javascript an array is just another object, these values won't be available in standard array iteration, and the length of the array will still be 0.

EDIT: You note that you're using jQuery's .ajax() function to post the data to the server. The .ajax() method expects an object containing key/value pairs, and sends them to the server as a GET or POST parameters. So in your case, if you're using an object as I describe above, your server will receive the parameters "id", "name", etc in the $_POST array - not a "data" parameter.

I suspect, though I haven't tested this, that using var data = new Array(); wouldn't work at all, because of the way jQuery serializes the data passed to .ajax() - even though an array is also an object, jQuery checks if it's an array and treats it differently:

If the object passed is in an Array, it must be an array of objects in the format returned by .serializeArray()

[{name:"first",value:"Rick"}, {name:"last",value:"Astley"}, {name:"job",value:"Rock Star"}]

So it wouldn't use the key/value pairs you set at all - you would be passing an empty array and no parameters would be passed to the server.

So the right approach here:

  1. Use var data = {};
  2. On the server, look for $_POST['id'], $_POST['name'], etc.
Sign up to request clarification or add additional context in comments.

8 Comments

Arrays are also objects, and all objects can be used as associative arrays in JavaScript.
up voted for suggesting a object notation, can you point to a comparison or an article that says why one should be opting for object notation rather than arrays in JS
@Kumar: You can use either, but using an array object for an associative array does not provide any advantages over a regular object.
@nrabinowitz but print_r(json_decode($_POST['data'])); does not shows up
@casablanca - yes, all Javascript objects are (more or less) associative arrays, but you can't iterate through them in the same way you'd iterate through an actual Array instance.
|
0

You're using the array like a regular object. You're augmenting the array with additional properties, but the array itself is still empty (you should have an array.length === 0, hence the null). Try changing

var data = new Array();

to

var data = {};

and see what you get.

1 Comment

No problem. Sorry I missed the second part of your question.

Your Answer

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.