发送POST数据到PHP而不使用HTML表单?
无论如何发送发布数据到一个PHP脚本以外的表单? (当然不使用GET)。发送POST数据到PHP而不使用HTML表单?
我希望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>
我发布了我在最终问题/ OP中使用的内容(不确定你在这里称之为什么) – 2011-12-27 00:26:05
您可以使用jQuery张贴到PHP页面: http://api.jquery.com/jQuery.post/
看起来我需要学习Ajax和JQuery来推进......我一直在努力做的很多事情看起来像是可以让他们更容易完成。然而,我从来没有碰过这样的短期内我不能真正使用它(直到我了解JQuery)。非常感谢。 – 2011-12-26 20:46:11
您可以发送你想要重新加载页面之前发布的数据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
如何:
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);
你可以,如果你想避免使用AJAX,都隐藏表单,或动态创建一个,只是提交超时后使用Javascript形式。 AJAX可能是最好的解决方案,它完全取决于你想要做什么,AJAX可以为你提供更多的空间来优雅地处理错误。 – DaveRandom 2011-12-26 20:53:54
啊是的,这听起来对我来说是可能的,我敢肯定其他的建议是更好的整体,但这是一个小项目,这个解决方案听起来像它适合我的编码风格:) – 2011-12-26 20:57:55
看到我的答案如何这可以是在无框架的Javascript中完成。 – DaveRandom 2011-12-26 21:19:31