是否可以使用javascript发送后变量?我希望id发送邮件,而不是得到.
window.location.href="hanteraTaBortAnvandare.php?id=10";
Run Code Online (Sandbox Code Playgroud)
Guf*_*ffa 14
最简单的方法是在页面中添加一个表单:
<form method="POST" action="hanteraTaBortAnvandare.php" id="DeleteUserForm">
<input type="hidden" name="id" value="10" />
</form>
Run Code Online (Sandbox Code Playgroud)
然后你只需发表表格:
document.getElementById("DeleteUserForm").submit();
Run Code Online (Sandbox Code Playgroud)
gio*_*ian 13
您可以使用表格然后 document.getElementById('id_of_the_form').submit();
表单不需要以纯HTML形式编写:您可以使用dinamically创建它:
function postIt() {
form = document.createElement('form');
form.setAttribute('method', 'POST');
form.setAttribute('action', 'someURL');
myvar = document.createElement('input');
myvar.setAttribute('name', 'somename');
myvar.setAttribute('type', 'hidden');
myvar.setAttribute('value', 'somevalue');
form.appendChild(myvar);
document.body.appendChild(form);
form.submit();
}
Run Code Online (Sandbox Code Playgroud)
您可以使用Ajax请求(或使用隐藏的表单) - 在这种情况下;
MooTools示例:
new Request({
url: 'hanteraTaBortAnvandare.php',
method: 'post',
data: {
'id': '10'
},
onComplete: function(response) {
alert(response);
}
});
Run Code Online (Sandbox Code Playgroud)
jQuery示例:
$.ajax({
type: 'post',
url: 'hanteraTaBortAnvandare.php',
data: 'id=10',
success: function(response) {
alert(response);
}
});
Run Code Online (Sandbox Code Playgroud)
当然你可以在没有外部库的情况下做到这一点,但它们简化了很多!
您可以通过 JavaScript 提交表单数据,但不能使用 window.location.href。更改 URL 位置总是会发出 GET。
您需要从 DOM 获取对表单的引用,然后向其发出 Submit() 调用。