Javascript 在不使用 HTML 表单的情况下将 POST 数据发送到 PHP?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8638984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:50:48  来源:igfitidea点击:

Send POST data to PHP without using an HTML form?

phpjavascripthtml

提问by bag-man

Is there anyway to send post data to a php script other than having a form? (Not using GET of course).

除了具有表单之外,是否还有将发布数据发送到 php 脚本的方法?(当然不使用 GET)。

I want javascript to reload the page after X seconds and post some data to the page at the same time. I could do it with GET but I would rather use POST, as it looks cleaner.

我希望 javascript 在 X 秒后重新加载页面并同时将一些数据发布到页面。我可以用 GET 来完成,但我更愿意使用 POST,因为它看起来更干净。

Thanks a lot.

非常感谢。

EDIT: Would it be possible to do with PHP header? I'm sure it is better to use JQuery but for my current situation I could implement that a lot easier/faster : )

编辑:是否可以使用 PHP 标头?我确信使用 JQuery 会更好,但对于我目前的情况,我可以更容易/更快地实现它:)

Cheers

干杯

I ended up doing it like so:

我最终这样做了:

<script>
  function mySubmit() {
    var form = document.forms.myForm;
    form.submit();
  }
</script>

...

<body onLoad="mySubmit()";>
  <form action="script.php?GET_Value=<?php echo $GET_var ?>" name="myForm" method="post">
    <input type="hidden" name="POST_Value" value="<?php echo $POST_Var ?>">
  </form>
</body>

Seems to work fine for me, but please say if there is anything wrong with it!

对我来说似乎工作正常,但请说它是否有任何问题!

Thanks everyone.

谢谢大家。

回答by DaveRandom

As requested above, here is how you could dynamically add a hidden form and submit it when you want to refresh the page.

如上所述,这里是如何动态添加隐藏表单并在您想要刷新页面时提交它。

Somewhere in your HTML:

在您的 HTML 中的某处:

<div id="hidden_form_container" style="display:none;"></div>

And some Javascript:

还有一些 Javascript:

function postRefreshPage () {
  var theForm, newInput1, newInput2;
  // Start by creating a <form>
  theForm = document.createElement('form');
  theForm.action = 'somepage.php';
  theForm.method = 'post';
  // Next create the <input>s in the form and give them names and values
  newInput1 = document.createElement('input');
  newInput1.type = 'hidden';
  newInput1.name = 'input_1';
  newInput1.value = 'value 1';
  newInput2 = document.createElement('input');
  newInput2.type = 'hidden';
  newInput2.name = 'input_2';
  newInput2.value = 'value 2';
  // Now put everything together...
  theForm.appendChild(newInput1);
  theForm.appendChild(newInput2);
  // ...and it to the DOM...
  document.getElementById('hidden_form_container').appendChild(theForm);
  // ...and submit it
  theForm.submit();
}

This is equivalent to submitting this HTML form:

这相当于提交此 HTML 表单:

<form action="somepage.php" method="post">
  <input type="hidden" name="input_1" value="value 1" />
  <input type="hidden" name="input_2" value="value 2" />
</form>

回答by box86rowh

You can use JQuery to post to a php page: http://api.jquery.com/jQuery.post/

您可以使用 JQuery 发布到 php 页面:http: //api.jquery.com/jQuery.post/

回答by user2749894

How about this:

这个怎么样:

    function redirectWithPostData(strLocation, objData, strTarget)
{
    var objForm = document.createElement('FORM');
    objForm.method = 'post';
    objForm.action = strLocation;

    if (strTarget)
        objForm.target = strTarget;

    var strKey;
    for (strKey in objData)
    {
        var objInput = document.createElement('INPUT');
        objInput.type = 'hidden';
        objInput.name = strKey;
        objInput.value = objData[strKey];
        objForm.appendChild(objInput);
    }

    document.body.appendChild(objForm);
    objForm.submit();

    if (strTarget)
        document.body.removeChild(objForm);
}

use like this:

像这样使用:

redirectWithPostData('page.aspx', {UserIDs: getMultiUserSelectedItems()},'_top');

回答by nafg

Use the FormDataAPI.

使用FormDataAPI。

From the example there:

从那里的例子:

var formData = new FormData();
formData.append("username", "Groucho");
formData.append("accountnum", 123456);

var request = new XMLHttpRequest();
request.open("POST", "http://foo.com/submitform.php");
request.send(formData);

回答by abuduba

By jQuery:

通过 jQuery:

$.ajax({
  url: "yourphpscript.php",
  type: "post",
  data: json/array/whatever,

  success: function(){ // trigger when request was successfull
    window.location.href = 'somewhere'
  },
  error: anyFunction // when error happened
  complete: otherFunction // when request is completed -no matter if the error or not
  // callbacks are of course not mandatory
})

or simplest:

或最简单的:

$.post( "yourphpscript.php", data, success_callback_as_above );

more on http://api.jquery.com/jQuery.ajax

更多关于http://api.jquery.com/jQuery.ajax

回答by tacos_tacos_tacos

Form your own header, as such:

形成您自己的标题,如下所示:

POST /submit.php HTTP/1.1
Host: localhost
User-Agent: Mozilla/4.0
Content-Length: 27
Content-Type: application/x-www-form-urlencoded

userId=admin&password=letmein

回答by PeeHaa

You can send an xhr request with the data you want to post before reloading the page.

您可以在重新加载页面之前发送带有要发布的数据的 xhr 请求。

And reload the page only if the xhr request is finished.

并且只有在 xhr 请求完成后才重新加载页面。

So basically you would want to do a synchronous request.

所以基本上你会想要做一个同步请求。