我如何使用jQuery / Javascript POST变量到另一个页面

How do I POST variables using jQuery / Javascript to another page?

本文关键字:变量 另一个 POST Javascript 何使用 jQuery      更新时间:2023-09-26

我如何使用POST使用jQuery/Javascript发送到另一个页面并重定向到该页面?

使用GET发送变量

在Javascript

window.location = 'receivepage.php?variable='+testVariable;

当它被PHP接收时…

$var = $_GET['variable'];

我怎么做^,使用$_POST?

I already tried…

$.post(
'receivepage.php', {
 i: i
 }, function(){
 window.location = 'receivepage.php';
 }
);

但是当它到达PHP

时,它似乎失去了变量

Doing $。post试图通过ajax发布信息,然后重定向到您的页面,所以当您最终到达那里时,属性"i"将不会被接收。

你可以这样做:

<form method="post" target="receivepage.php" id="myform">
   <input type="hidden" name="i" value="blah" />
</form>

JS

<script type="text/javascript">
  $("#myform").submit();
</script>

那解决你的问题了吗?

编辑

如果你的值来自JS,你可以这样添加:

JS

<script type="text/javascript">
  $('#myform input[name="i"]').val(i);
  $("#myform").submit();
</script>

根据您的示例,"i"是在窗口作用域中定义的,使其成为全局的,并且可以从该脚本访问。

在您的post示例中,$_POST['i']将是您的变量