javascript - How do I create an associate array in jquery and send it via ajax to get parsed with php? - Stack Overflow

How would I make an associative array (or some parable alternative) in jQuery AND send that array via a

How would I make an associative array (or some parable alternative) in jQuery AND send that array via ajax to a php page so that I can use php to handle it?

Something like this...

// jQuery

if($something == true) {
    data[alt] = $(this).attr('alt');
    data[src] = $(this).attr('src');
else if ($something == "something else") {
    data[html] = $(this).html();
}

Then, send this array using the .ajax() function

// jQuery

$.ajax({
    data: /* somehow send my array here */,
    type: 'POST',
    url: myUrl,
    plete: function(){
        // I'll do something cool here
    }
});

Finally, parse this data with php...

// php

<img alt="<?PHP echo $_POST['alt']; ?>" src="<?PHP echo $_POST['src']; ?>" />

I've done a little googling on the subject and have read that you cannot make an associative array with javascript so I'm really just looking for some alternative. Thanks in advance.

How would I make an associative array (or some parable alternative) in jQuery AND send that array via ajax to a php page so that I can use php to handle it?

Something like this...

// jQuery

if($something == true) {
    data[alt] = $(this).attr('alt');
    data[src] = $(this).attr('src');
else if ($something == "something else") {
    data[html] = $(this).html();
}

Then, send this array using the .ajax() function

// jQuery

$.ajax({
    data: /* somehow send my array here */,
    type: 'POST',
    url: myUrl,
    plete: function(){
        // I'll do something cool here
    }
});

Finally, parse this data with php...

// php

<img alt="<?PHP echo $_POST['alt']; ?>" src="<?PHP echo $_POST['src']; ?>" />

I've done a little googling on the subject and have read that you cannot make an associative array with javascript so I'm really just looking for some alternative. Thanks in advance.

Share Improve this question asked Aug 17, 2010 at 1:47 JohnJohn 3,9166 gold badges34 silver badges39 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

You can pass the data as an object to $.ajax(), like this:

var data = {};
if ($something == true) {
    data.alt = $(this).attr('alt');
    data.src = $(this).attr('src');
}else if ($something == "something else") {
    data.html = $(this).html();
}

$.ajax({
    data: data,
    type: 'POST',
    url: myUrl,
    plete: function(){
        // I'll do something cool here
    }
});

This will get serialized for the post, internally using $.param(obj) to convert it for the POST, for example:

alt=thisAlt&src=thisSrc

or:

html=myEncodedHtml

Won't it be simpler to just send some json to the php side, then use the json_decode function in php to get an associative array on the php side?

Associative array is sortof a PHP thing. But you can have something similar via curly braces ({}). In fact, you're already using it in your $.ajax() call. Notice the '{}' parts. In this case, you can use json_decode() in the PHP server to decode the 'data' parameter:

 $.ajax({  
     url: myUrl,  
     data: {   foo: 0,   bar: 1,   baz: 2   }, 
     success: function() {   }, 
     dataType: 'json'  
 });

`

Using json_decode() will get you something like:

php array('foo' => 0, 'bar' => 1, 'baz' => 2);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745255995a4618959.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信