单击按钮时使用 javascript 将 php post 变量发送到 url

send php post variable to url using javascript when button is clicked

我有一个 php 页面,它使用 post 方法获取 $page 和 $user,我还有一个我想要的按钮,可以在同一个 window 在点击时使用 $page 和 $user 变量,将它们与 $_GET[] 函数一起使用。 我希望我的 URL 像:

http://www.test.com/test.php?page=$page&user=$user

我的代码是这样的:

<?php
$page=$POST_['page'];
$user=$POST_['user'];
<?


<html>
<head>

function openurl() {
var user=<?php echo "$user";?>;
var page=<?php echo "$page";?>;
open('www.test.com/test.php?page='+page'&user='+user,'_self');

}

</head>
<body>

<button onclick="openurl()" type="button">open url</button>


</body>
</html>

  1. 您可以创建一个表单,然后通过 Javascript 运行 YOURFORMNAME.submit();
  2. 在javaScript中使用href移动到另一个位置:

    location.href="www.test.com/test.php?page='+page'&user='+user"

根本不需要脚本

如果你想要GET:

<?php
$page=$GET_['page']; // should be sanitized and you can use REQUEST for either 
$user=$GET_['user'];
$parm = "page=".$page."&user=".$user;
?>
<a href="http://www.test.com/test.php?<?php echo $parm; ?>" class="button">Open URL</a>

如需post:

<form action="test.php" method="post">
<input type="hidden" name="page" value="<?php echo $page; ?>"/>
<input type="hidden" name="user" value="<?php echo $user; ?>"/>
<input type="submit" value="Open URL" />
</form>

更改这些行:

<?php
$page=$POST_['page']; 
$user=$POST_['user']; 
<?

....

var user=<?php echo "$user";?>; 
var page=<?php echo "$page";?>; 
open('www.test.com/test.php?page='+page'&user='+user,'_self');

对此:

<?php
$page=$_POST['page']; //incorrect $_POST declaration
$user=$_POST['user']; //incorrect $_POST declaration
?> //php tag incorrectly closed

....

var user=<?php echo $user;?>; //echoing a variable not string (no need for quotes)
var page=<?php echo $page;?>; // echoing a variable not string (no need for quotes)
open('www.test.com/test.php?page='+page+'&user='+user,'_self'); // link was broken, forget to put '+' after page variable in link.