English 中文(简体)
支离破碎的多重数据
原标题:Sending multiple data in jquery

i) 具有发送数据守则

  var test={imagename:"apple.jpg",x:"13",y:"33"};

  $.ajax({
 type: "POST",
     url: "some.php",
     data: test,
     success: function(response){
    console.log(response);
    }
   });

但是,现在我想向综合系统提供多种数据。 我的《加拿大法典》就是这样。

print $_POST[ imagename ];

i think of an approach like this var test={imagename:"apple.jpg|hen.jpg",x:"13|44",y:"33|22"}; and on php getting $_POST[ imagename ] then split it according to | but i am not liking semantic approach of it.
1-Does any one know better solution?
2-Please provide both javascript,jquery and php code for answer.
3-One final question is this notation called json var test={imagename:"apple.jpg",x:"13",y:"33"};
Thanks

最佳回答

一个阵列是这方面最有意义的解决办法,如 Java文:

var data = [
    {imagename:"apple1.jpg", x:"13", y:"33"},
    {imagename:"apple2.jpg", x:"51", y:"84"},
    {imagename:"apple3.jpg", x:"79", y:"57"}
];

修改如下:

$.ajax({
     type: "POST",
     url: "some.php",
     data: {data: data},
     success: function(response){
         console.log(response);
     }
 });

并且,在公共卫生和社会福利部中,你能够做到:

<?
    print_r($_POST[ data ]); // dumps an array

    $filename1 = $_POST[ data ][0][ filename ]; // filename of item #1
?>

最后,var test={imagename: “apple.jpg”,x:“13”,y:“33”};只不过是一些 Java成文法。 页: 1 JSON。 尽管JSON像Javales(JS甚至位于JSON的Javagust),但它只不过是你再次寄出的特性。 JSON是数据转让的一种格式。 当你“无包装”时(在 Java或PHP),就不再叫JSON。

问题回答

@pimvdb:

foreach($_POST[ data ] as $data) {
    $filename = $data[ filename ];
    $width = $data[ x ];
    $height = $data[ y ];

    do_something_with_it($filename, $width, $height);
}

您可尝试使用nes物体,如:

var test={imagename:["apple.jpg","hen.jpg"],x:["13","44"],y:["33","22"]};

  $.ajax({
     type: "POST",
     url: "some.php",
     data: test,
     success: function(response){
       console.log(response);
     }
  });

Then from the PHP side, you should be able to access it as an array, by calling:

  $_POST[ image name ][index_num_here];

  Example:
  $_POST[ image name ][0] => "apple.jpg"

Use arrays. Javascript:

var data = {
    "images": [
          {name: "apple.jpg", x: 13, y:14},
          {name: "egg.jpg", x:14, y: 35}
    ]
};

$.ajax({
   type: "POST",
   url: "some.php",
   data: data,
   success: function(response){
     console.log(response);
   }
 });

PHP:

// First.
print $_POST[ images ][0][ name ] // Prints apple.jpg
// Second
print $_POST[ images ][1][ name ] // Prints egg.jpg

// Looping
foreach($_POST[ images ] as $image) {
    print $image[ name ];
    print $image[ x ];
    print $image[ y ];
}

您的榜样

var test={imagename:"apple.jpg",x:"13",y:"33"};

The following part is considered as JSON (JavaScript Object Notation):

{imagename:"apple.jpg",x:"13",y:"33"}

Every JSON string is valid JavaScript as well.





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签