简体   繁体   English

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

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

I have a php page which gets $page and $user using post method, I also have a button that i want, to open a URL in the same window using $page and $user variables when clicked, to use them with $_GET[] function. 我有一个php页面,它使用post方法获取$ page和$ user,还有一个我想要的按钮,当单击时使用$ page和$ user变量在同一窗口中打开URL,以将它们与$ _GET [ ]功能。 i want my URL be like: 我希望我的网址是这样的:

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

my code is like this: 我的代码是这样的:

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

There is no need for scripting at all 根本不需要脚本

If you want 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>

If you need to 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>
  1. You can create a form and via Javascript just run YOURFORMNAME.submit(); 您可以创建一个表单,然后通过Javascript运行YOURFORMNAME.submit();。
  2. Use href in javaScript to move to another location: 在javaScript中使用href移至另一个位置:

    location.href="www.test.com/test.php?page='+page'&user='+user" location.href = “www.test.com/test.php?page = '+页' &用户='+用户”

Change these lines: 更改这些行:

<?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');

to this: 对此:

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

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

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