PHP - 在提交之前添加表单元素 - 购买按钮
我需要在单击“提交”按钮后但在将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以这样尝试:
首先通过将提交按钮类型从“提交”更改为“按钮”(或其他)来禁用提交
将该按钮上的
onclick
放入 JavaScript 例程(这里我使用submit_form()
)。在表单中创建一个空 div。 (这里我用
id = 'dynamic'
来称呼它)使用 jquery,这是
submit_form()
。我认为在提交之前您需要给这些元素一些时间来正确绑定。也许在
$("#myForm").submit()
;之前有短暂的延迟,
以下是
submit_form()
函数的代码:You can try it this way:
First disable the submit by changing the submit button type from 'submit' to 'button' (or whatever)
Put in
onclick
on that button to a javascript routine (here i usesubmit_form()
).Create an empty div within your form. (here i call it with
id = 'dynamic'
)Using jquery, this is the
submit_form()
.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:您可以将数据发布到您的服务器,然后再次将其附加到附加的新元素发布到外部服务器。
您的工作是在服务器端完成的。
另请参阅:
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?
如果您需要对提交到其他服务器的内容进行任何控制,则必须自己执行此操作。将表单提交到您自己的服务器,然后验证它,添加您的数据并重新提交到其他服务器。
您可以使用 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.