简体   繁体   English

如何使用POST方法和JS submit()传递表单数据?

[英]How to pass form data using POST method and JS submit()?

How to pass form data using POST method and JS submit() ? 如何使用POST方法和JS submit()传递表单数据?

I would like not to use AJAX. 我不想使用AJAX。

I can achieve that by using JS and GET method, but my goal now is to use JS (probably with submit() ) and POST method. 我可以通过使用JS和GET方法实现这一点,但我现在的目标是使用JS(可能使用submit())和POST方法。

Please give a simple working example. 请举一个简单的工作示例。

Thank you! 谢谢!

Edited: Sorry, I will be more specific. 编辑:对不起,我会更具体。 Here is a simple code I have: 这是一个简单的代码:

<form name=\"myform\" action=\"\">
  Search: <input type='text' name='query' />
  <a href=\"javascript: submitform()\">Search</a>
</form>

 <script type=\"text/javascript\">
 function submitform() {   document.myform.submit(); }
 </script>

When I insert "word" into the input field and press Search, the URL becomes "../index.php?query=word" and that means that form data is passed like GET method. 当我在输入字段中插入“word”并按Search时,URL变为“../index.php?query=word”,这意味着表单数据像GET方法一样传递。

And my goal is to remove any form data from URL and pass it like with POST method. 我的目标是从URL中删除任何表单数据并像POST方法一样传递它。

Edited: ohh.. I just added method=post and no form data in the URL anymore :) 编辑:哦..我刚刚在URL中添加了method = post和没有表单数据:)

Just have a form and submit it. 只需要一个表格并提交即可。

form = document.forms[0] //assuming only form.
form.submit();

EDIT: OP has clarified question 编辑:OP澄清了问题

Specify a method attribute on your form. 在表单上指定method属性。

<form name="myform" action="" method="POST">

It will cause the form to be submitted as a POST. 它将导致表单作为POST提交。

Set the form's method attribute to POST: 将表单的method属性设置为POST:

<form method="post">

And then submit the form via JavaScript using <formelement>.submit() 然后使用<formelement>.submit()通过JavaScript提交表单

As mentioned already, you can use a form. 如前所述,您可以使用表单。 I find it useful to have a function which creates a form on-the-fly and submits it - that way you don't need to clutter your markup with forms until they're needed by your JS. 我发现有一个函数可以动态创建一个表单并提交它 - 这样你就不需要用表单来混淆你的标记,直到你的JS需要它们为止。

function PostObjectToUri(uri, obj) {
    "use strict";

    var json, form, input;

    json = JSON.stringify(obj);

    form = document.createElement("form");
    form.method = "post";
    form.action = uri;
    input = document.createElement("input");
    input.setAttribute("name", "json");
    input.setAttribute("value", json);
    form.appendChild(input);
    document.body.appendChild(form);
    form.submit();
};

Obviously that example JSON-serializes an object into a string and sets it on a single input, but you could adapt it to do whatever you need. 显然,示例JSON将对象序列化为字符串并将其设置在单个输入上,但您可以根据需要调整它。

EDIT : I'm not actually sure whether you even need to append to the DOM before submitting - can anyone clarify? 编辑 :我不确定你是否需要在提交之前附加到DOM - 有人可以澄清吗?

jQuery AJAX post is best way to do it. jQuery AJAX帖子是最好的方法。 See below example code. 见下面的示例代码。

e.preventDefault();

$.ajax({
   type: 'post',
   url: 'data.php',
   data: $('form').serialize(),
   success: function(response) {
      $('#DisplayDiv').html(response);
   }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM