除了表单之外,还有什么方法可以将发布数据发送到 php 脚本吗? (当然不使用 GET)。
我希望 javascript 在 X 秒后重新加载页面并同时将一些数据发布到页面。我可以使用 GET 来完成,但我宁愿使用 POST,因为它看起来更干净。
非常感谢。
编辑:是否可以使用 PHP header ?我确信使用 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>
对我来说似乎工作正常,但如果有任何问题请指出!
谢谢大家
最佳答案
按照上面的要求,这里是您可以动态添加隐藏表单并在您想要刷新页面时提交它的方法。
HTML 中的某处:
<div id="hidden_form_container" style="display:none;"></div>
还有一些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();
}
这相当于提交这个 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>
关于php - 在不使用 HTML 表单的情况下将 POST 数据发送到 PHP?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8638984/