不使用HTML表单将POST数据发送到PHP?

bag*_*man 14 html javascript php

无论如何将帖子数据发送到PHP脚本而不是表单?(当然不使用GET).

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

非常感谢.

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

干杯

我最终这样做了:

<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>
Run Code Online (Sandbox Code Playgroud)

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

感谢大家.

Dav*_*dom 21

如上所述,以下是您可以动态添加隐藏表单并在想要刷新页面时提交它的方法.

HTML中的某处:

<div id="hidden_form_container" style="display:none;"></div>
Run Code Online (Sandbox Code Playgroud)

还有一些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();
}
Run Code Online (Sandbox Code Playgroud)

这相当于提交此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>
Run Code Online (Sandbox Code Playgroud)


box*_*owh 5

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