[英]Send POST data to PHP without using an HTML form?
无论如何将帖子数据发送到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>
似乎对我来说工作正常,但请说出来是否有任何问题!
感谢大家。
如上所述,以下是您可以动态添加隐藏表单并在想要刷新页面时提交它的方法。
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>
您可以使用JQuery发布到php页面: http : //api.jquery.com/jQuery.post/
这个怎么样:
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);
}
使用这样:
redirectWithPostData('page.aspx', {UserIDs: getMultiUserSelectedItems()},'_top');
使用FormData API。
从那里的例子:
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);
通过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
})
或最简单:
$.post( "yourphpscript.php", data, success_callback_as_above );
形成自己的标题,如下:
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
在重新加载页面之前,您可以发送包含要发布的数据的xhr请求。
只有在xhr请求完成后才重新加载页面。
所以基本上你想要做同步请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.