发送POST数据到PHP而不使用HTML表单? [英] Send POST data to PHP without using an HTML form?
问题描述
我希望javascript在X秒后重新加载页面,并同时向页面发布一些数据。我可以用GET来做,但我宁愿使用POST,因为它看起来更干净。
非常感谢。
编辑:使用PHP标题可以吗?我确信使用JQuery会更好,但是对于我目前的情况,我可以更容易/更快地实现它:)
干杯
我最终这样做了:
< script>
函数mySubmit(){
var form = document.forms.myForm;
form.submit();
}
< / script>
...
< body onLoad =mySubmit();>
< form action =script.php?GET_Value =<?php echo $ GET_var?> name =myFormmethod =post>
< input type =hiddenname =POST_Valuevalue =<?php echo $ POST_Var?>>
< / form>
< / body>
似乎对我很好,但请说出是否有任何问题!
谢谢大家。
添加一个隐藏的表单并在您想刷新页面时提交。
HTML中的某处:
< div id =hidden_form_containerstyle =display:none;>< / div>
还有一些Javascript:
函数postRefreshPage(){
var theForm,newInput1,newInput2;
//首先创建一个< form>
theForm = document.createElement('form');
theForm.action ='somepage.php';
theForm.method ='post';
//接下来在表单中创建< input> s并赋予它们名称和值
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';
//将所有内容放在一起...
theForm.appendChild(newInput1);
theForm.appendChild(newInput2);
// ...并将其添加到DOM ...
document.getElementById('hidden_form_container')。appendChild(theForm);
// ...并提交
theForm.submit();
}
这相当于提交这个HTML表单:
< form action =somepage.phpmethod =post>
< input type =hiddenname =input_1value =value 1/>
< input type =hiddenname =input_2value =value 2/>
< / form>
Is there anyway to send post data to a php script other than having a form? (Not using GET of course).
I want javascript to reload the page after X seconds and post some data to the page at the same time. I could do it with GET but I would rather use POST, as it looks cleaner.
Thanks a lot.
EDIT: Would it be possible to do with PHP header? I'm sure it is better to use JQuery but for my current situation I could implement that a lot easier/faster : )
Cheers
I ended up doing it like so:
<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>
Seems to work fine for me, but please say if there is anything wrong with it!
Thanks everyone.
As requested above, here is how you could dynamically add a hidden form and submit it when you want to refresh the page.
Somewhere in your HTML:
<div id="hidden_form_container" style="display:none;"></div>
And some 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();
}
This is equivalent to submitting this HTML form:
<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>
这篇关于发送POST数据到PHP而不使用HTML表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!