不使用HTML表单将POST数据发送到PHP?
除了使用表单之外,是否还可以向php脚本发送post数据?(当然不使用GET) 我希望javascript在X秒后重新加载页面,同时向页面发布一些数据。我可以用GET做,但我宁愿用POST,因为它看起来更干净 非常感谢 编辑:可以使用PHP头吗?我相信使用JQuery会更好,但就我目前的情况而言,我可以更容易/更快地实现它:) 干杯 我最终还是这样做的:不使用HTML表单将POST数据发送到PHP?,php,javascript,html,Php,Javascript,Html,除了使用表单之外,是否还可以向php脚本发送post数据?(当然不使用GET) 我希望javascript在X秒后重新加载页面,同时向页面发布一些数据。我可以用GET做,但我宁愿用POST,因为它看起来更干净 非常感谢 编辑:可以使用PHP头吗?我相信使用JQuery会更好,但就我目前的情况而言,我可以更容易/更快地实现它:) 干杯 我最终还是这样做的: <script> function mySubmit() { var form = document.forms.my
<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>
函数mySubmit(){
var form=document.forms.myForm;
表单提交();
}
...
您可以使用JQuery发布到php页面:
在重新加载页面之前,您可以发送包含要发布的数据的xhr请求
并且仅当xhr请求完成时才重新加载页面
因此,基本上您希望执行同步请求。通过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
按照上面的要求,下面是如何动态添加隐藏表单,并在需要刷新页面时提交它
在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();
}
函数postRefreshPage(){
变量形式,newInput1,newInput2;
//首先创建一个
theForm=document.createElement('form');
theForm.action='somepage.php';
form.method='post';
//接下来,在表单中创建s,并给它们命名和值
newInput1=document.createElement('input');
newInput1.type='隐藏';
newInput1.name='input_1';
newInput1.value='value 1';
newInput2=document.createElement('input');
newInput2.type='隐藏';
newInput2.name='input_2';
newInput2.value='value 2';
//现在把一切都放在一起。。。
appendChild格式(newInput1);
appendChild格式(newInput2);
//…并将其提交给DOM。。。
document.getElementById('hidden_form_container').appendChild(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>
这个怎么样:
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');
使用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);
看来我需要学习Ajax和JQuery来提高。。。我一直试图做的很多事情看起来都可以用他们来做得更容易。但是我也从未接触过,所以短期内我无法真正使用它(直到我了解JQuery)。非常感谢。如果您想避免使用AJAX,可以隐藏表单,或者动态创建表单,并在超时后使用Javascript提交表单。AJAX可能是最好的解决方案,这完全取决于您想要做什么,而且AJAX可以为您提供更多优雅地处理错误的空间,我相信其他建议总体上更好,但这是一个小项目,这个解决方案听起来很适合我的编码风格:)请参阅下面我的答案,了解如何在无框架Javascript中实现这一点的详细信息。我在最后的问题/OP中发布了我使用的内容(不确定您在这里如何称呼它)