简体   繁体   English

如何使用jQuery / Javascript将变量发布到另一页?

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

How do I use POST using jQuery/Javascript to send to another page and redirect to that page? 如何使用使用jQuery / Javascript的POST发送到另一个页面并重定向到该页面?

Sending variables using GET... 使用GET发送变量...

In Javascript 在Javascript中

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

When it is receive by PHP... 当它被PHP接收时...

$var = $_GET['variable'];

How do I do that ^ , using $_POST? 如何使用$ _POST来完成^?

I already tried... 我已经试过......

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

but it seems to lose the variable when it reaches PHP 但是当它到达PHP时似乎丢失了该变量

Doing $.post is trying to post the information via ajax, and THEN redirecting to your page, so when you finally get there, the attribute "i" won't be received. 进行$ .post尝试通过ajax发布信息,然后重定向到您的页面,因此,当您最终到达页面时,将不会接收到属性“ i”。

You could do someothing like this: 您可以这样做:

HTML HTML

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

JS JS

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

Does that solve your problem? 这样可以解决您的问题吗?

Edit 编辑

If your value comes from JS, you can add it like this: 如果您的价值来自JS,则可以这样添加:

JS JS

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

According to your example, "i" is defined on the window scope, making it global and accessible from this script. 根据您的示例,“ i”是在窗口范围内定义的,从而使其成为全局的并且可以从此脚本访问。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM