PHP - 在提交之前添加表单元素 - 购买按钮

发布于 2024-10-05 03:38:53 字数 208 浏览 0 评论 0原文

我需要在单击“提交”按钮后但在将 POST 数据发送到服务器之前将表单元素动态添加到 HTML 表单中。新元素必须从我的服务器上的 PHP 文件“读取”。

历史: 目前,我的 HTML 表单具有“隐藏”字段,这些字段会提交到另一台服务器进行处理。我无法控制其他服务器。我的问题是任何人都可以编辑这些隐藏字段。

如何在表单提交后立即将表单元素动态添加到 POST 数据中?

I need to dynamically add form elements to an HTML form as soon as the Submit button is clicked but before the POST data is sent to a server. The new elements must be "read" from a PHP file on my server.

HISTORY:
Currently my HTML form has "hidden" fields that are submitted to another server for processing. I have no control over the other server. My problem is that anyone can edit these hidden fields.

How can I dynamically add form elements to the POST data as soon as the form is submitted?

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

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

发布评论

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

评论(3

蓝天 2024-10-12 03:38:53

您可以这样尝试:

  1. 首先通过将提交按钮类型从“提交”更改为“按钮”(或其他)来禁用提交

  2. 将该按钮上的 onclick 放入 JavaScript 例程(这里我使用 submit_form())。

  3. 在表单中创建一个空 div。 (这里我用 id = 'dynamic' 来称呼它)

  4. 使用 jquery,这是 submit_form()

  5. 我认为在提交之前您需要给这些元素一些时间来正确绑定。也许在 $("#myForm").submit();

    之前有短暂的延迟,

以下是 submit_form() 函数的代码:

function submit_form()
  {
    $("#dynamic").append("<input type='hidden' name='input1' value='whatever'>");
    $("#dynamic").append("<input type='hidden' name='input2' value='whatever'>");
    $("#myForm").submit();
  } 

You can try it this way:

  1. First disable the submit by changing the submit button type from 'submit' to 'button' (or whatever)

  2. Put in onclick on that button to a javascript routine (here i use submit_form()).

  3. Create an empty div within your form. (here i call it with id = 'dynamic')

  4. Using jquery, this is the submit_form().

  5. I think you will need to give it some time for these elements to bind properly before submitting. Maybe a short time delay before $("#myForm").submit();

Here is the code for the submit_form() function:

function submit_form()
  {
    $("#dynamic").append("<input type='hidden' name='input1' value='whatever'>");
    $("#dynamic").append("<input type='hidden' name='input2' value='whatever'>");
    $("#myForm").submit();
  } 
活雷疯 2024-10-12 03:38:53

您可以将数据发布到您的服务器,然后再次将其附加到附加的新元素发布到外部服务器。

您的工作是在服务器端完成的。

另请参阅:

php 服务器到服务器帖子?

You can post the data to your server and after it post again to the external server with the new elements attached.

Your job is done on server side.

See also:

php server-to-server post?

夜还是长夜 2024-10-12 03:38:53

如果您需要对提交到其他服务器的内容进行任何控制,则必须自己执行此操作。将表单提交到您自己的服务器,然后验证它,添加您的数据并重新提交到其他服务器。

您可以使用 PHP 中的 CURL 扩展从服务器发布数据。

If you need any control over what is submitted to the other server, you have to do that yourself. Make the form submit to your own server, then validate it, add your data and re-submit it to the other server.

You can use the CURL extension in PHP to post data from your server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文