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

时间:2015-02-05 10:18:49

标签: javascript php get onclick openurl

我有一个使用post方法获取$ page和$ user的php页面,我还有一个我想要的按钮,在点击时使用$ page和$ user变量在同一个窗口中打开一个URL,以便与它们一起使用$ _GET []函数。 我希望我的网址如下:

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>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

根本不需要编写脚本

如果您想要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>

如果您需要发布:

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

答案 1 :(得分:0)

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

    location.href = “www.test.com/test.php?page = '+页' &安培;用户='+用户”

答案 2 :(得分:0)

更改这些行:

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