简体   繁体   中英

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?

Sending variables using GET...

In Javascript

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

When it is receive by PHP...

$var = $_GET['variable'];

How do I do that ^ , using $_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

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.

You could do someothing like this:

HTML

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

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

<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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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