如何通过 $.post 将 javascript 对象数组发送到 php 脚本?

发布于 2024-08-07 03:42:57 字数 829 浏览 4 评论 0原文

我的页面上有一个数据表。这些列是“开始”、“结束”、“注释”和“选项”。选项列中有一个用于删除行的按钮,以及一个使表中的行可编辑的按钮。这允许更改“start”、“end”和“comment”的值。再次单击它即可完成编辑过程。最后,有一种方法可以向表中添加另一行。所有这一切都按预期进行。

我想在底部添加一个按钮,用于根据表中的数据创建一个数组,如下所示(表示为 JSON 纯粹是为了便于向 SO 显示:不需要):

[
    {
        "start"     :"2009/08/01",
        "end"       :"2009/08/08",
        "comment"   :"an example date.",
    },
    {
        "start"     :"2009/07/01",
        "end"       :"2009/07/08",
        "comment"   :"another example date, a month earlier.",
    },
    {
        "start"     :"2000/07/01",
        "end"       :"2000/07/08",
        "comment"   :"another example date, a year earlier. You get the idea.",
    }
]

我应该不会在构建时遇到太多麻烦数组,但是一旦有了,如何将其发布到后端 php 脚本(然后使用 put_csv() 将数据写入文件)?理想情况下,它将以数组格式在服务器端提供,以允许验证。

如果需要的话,我很乐意使用 jquery。

I have a table of data on my page. The columns are "start", "end", "comment", and "options". Within the options column is a button to delete the row, and a button that makes a row of the table editable. This allows the values of "start", "end", and "comment" to be changed. Clicking it again finishes the editing process. Finally, there is a way to add another row to the table. All of this works as expected.

I want to add a button at the bottom that creates an array out of the data in the table, that looks like this (represented as JSON purely for ease of showing to SO: not required):

[
    {
        "start"     :"2009/08/01",
        "end"       :"2009/08/08",
        "comment"   :"an example date.",
    },
    {
        "start"     :"2009/07/01",
        "end"       :"2009/07/08",
        "comment"   :"another example date, a month earlier.",
    },
    {
        "start"     :"2000/07/01",
        "end"       :"2000/07/08",
        "comment"   :"another example date, a year earlier. You get the idea.",
    }
]

I shouldn't have too much trouble building the array, but once I have, how can I post it to a backend php script (which will then use put_csv() to write the data to a file)? Ideally, it would be availiable server-side in a array format, to allow validation.

I'm happy to use jquery, if it's required.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

耀眼的星火 2024-08-14 03:42:57

如何将其结束到服务器取决于您想要归档的内容。如果您想加载一个将处理数据的新页面,您可以使用带有隐藏输入的常用表单。或者,如果您想通过 AJAX 在后台进行处理,请使用 jQuery.ajax 正如 CRasco 所说。无论使用哪种方式,都需要将结构化数组转换为简单的键/值列表。

一种选择是生成 JSON 文档(我认为 jQuery 没有为此提供功能,但您可以轻松找到一些东西)并在序列化文档中使用单个字段。然后在服务器端,您将使用 json_decode 来取回数组。如果您使用 AJAX 方法,jQuery.ajax 调用中的数据选项将包含:

{
    'data': '[{"start":"2009/08/01","end":"2009/08/08","comment":"an example date."},...'
}

更好的方法是将其序列化为 PHP 本身可以理解的格式的字段列表。这意味着您无需执行任何特殊操作,$_POST['data'] 将包含该数组。例如:

{
    'data[0][start]': '2009/08/01',
    'data[0][end]': '2009/08/08',
    'data[0][comment]': 'an example date.',
    'data[1][start]': '2009/07/01',
    'data[1][end]': '2009/07/08',
    'data[1][comment]': 'another example date, a month earlier.',
    ... 
}

如果您使用表单方法,则需要生成如下输入字段:

<input type="hidden" name="data[0][start]" value="2009/08/01" />

How you end it to the server depends on what do you want to archieve. If you want to load a new page, which will process the data, you can use an usual form with hidden inputs. Alternatively, if you want to process is in the background via AJAX, use jQuery.ajax as CRasco said. Either way you use, you need to convert the structured array into a simple key/value list.

One option is to generate a JSON document (I don't think jQuery provides a function for this, but you can easily find something) and use a single field with the serialized document. Then on the server side you would use json_decode to get the array back. If you are using the AJAX method, the data option in jQuery.ajax call would contain:

{
    'data': '[{"start":"2009/08/01","end":"2009/08/08","comment":"an example date."},...'
}

A better approach would be to serialize it into a list of fields in a format that PHP natively understands. That means you have to do nothing special, and $_POST['data'] will contain the array. For example:

{
    'data[0][start]': '2009/08/01',
    'data[0][end]': '2009/08/08',
    'data[0][comment]': 'an example date.',
    'data[1][start]': '2009/07/01',
    'data[1][end]': '2009/07/08',
    'data[1][comment]': 'another example date, a month earlier.',
    ... 
}

If you are using the form method, you need to generate input fields like:

<input type="hidden" name="data[0][start]" value="2009/08/01" />
锦爱 2024-08-14 03:42:57

我肯定会使用 jQuery。这是 $.post() 函数的页面。

http://docs.jquery.com/Ajax/jQuery.post

一个警告是$.post() 是 $.ajax() 函数的包装器,它经过简化,并且消除了您为除成功帖子之外的任何内容设置回调的能力。对于通过 AJAX 进行的重要事务,我将使用:

http://docs.jquery.com/Ajax /jQuery.ajax

一个例子是:

$.ajax({
      url: "script.php",
      global: false,
      type: "POST",
      data: ({id : this.getAttribute('id')}),
      dataType: "html",
      success: function(msg){
         alert(msg);
      }
   });

I would definitely use jQuery. Here is the page for the $.post() function.

http://docs.jquery.com/Ajax/jQuery.post

One caveat is that $.post() is a wrapper for the $.ajax() function that is simplified and eliminates your ability to set callbacks for anything but a successful post. For important transactions via AJAX, I would use this:

http://docs.jquery.com/Ajax/jQuery.ajax

An example being:

$.ajax({
      url: "script.php",
      global: false,
      type: "POST",
      data: ({id : this.getAttribute('id')}),
      dataType: "html",
      success: function(msg){
         alert(msg);
      }
   });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文