php 使用 onclick 发布变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14133502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Post variable with onclick?
提问by gator
How can I $_POST information from one page to another by clicking a link? No forms or submit boxes, just if the end user clicks on a link, it will post pertinent information in the opened link.
如何通过单击链接将信息从一个页面 $_POST 到另一个页面?没有表单或提交框,只要最终用户点击链接,它就会在打开的链接中发布相关信息。
Pseudocode, but:
伪代码,但是:
//On first page
<a href="./page.php" onclick="post" value="var">Click me!</a>
...
//On the page the above links to
$variable = $_POST["var"];
Something that I considered is the following, which although less than pretty, works. I'd like it so it's a post and not a get.
我考虑的是以下内容,虽然不那么漂亮,但有效。我喜欢它,所以它是一个帖子而不是一个获取。
//On first page
<a href="./page.php?var=<?php echo $variable; ?>">Click me!</a>
...
//On second page
$variable = $_GET["var"];
回答by Surinder ツ
Try this :
尝试这个 :
<a href="link.php" class="post">submit content using link</a>
<script type="text/javascript">
jQuery(document).ready(function($){
$(".post").on("click",function(){
$.ajax({
url: "http://www.yourwebsite.com/page.php",
type: "POST",
data: { name: "John", location: "Boston" },
success: function(response){
//do action
},
error: function(){
// do action
}
});
});
});
</script>
回答by cryptic ツ
Not sure why you don't do this:
不知道你为什么不这样做:
//On first page
//在第一页
<a href="./page.php?var=value">Click me!</a>
... //On the page the above links to
... //在页面上面链接到
$variable = $_GET['var'];
UPDATE:
更新:
As per your above comment:
根据您的上述评论:
GET displays the information in the URL, whereas with POST it does not. I do not wish an end user to just edit the URL with whatever value they wish. @popnoodles, when the user would click the hyperlink, it would direct to a new page. – riista
GET 显示 URL 中的信息,而 POST 则不显示。我不希望最终用户只用他们想要的任何值来编辑 URL。@popnoodles,当用户点击超链接时,它会指向一个新页面。– 里斯塔
You are trying to do this for securityreasons, as such neitherapproach is a good way to do it using GET or AJAX is bothunsafe as bothcan be tampered with. You need to re-think what you are trying to protect and how you can check if the data submitted is valid.
您出于安全原因尝试这样做,因此这两种方法都不是使用 GET 或 AJAX 执行此操作的好方法,两者都不安全,因为两者都可以被篡改。您需要重新考虑您要保护的内容以及如何检查提交的数据是否有效。
回答by Your Common Sense
To choose between GET and POST is simple
在 GET 和 POST 之间进行选择很简单
- POST should be used to modify state of the server.
- all other cases goes for GET
- POST 应该用于修改服务器的状态。
- 所有其他情况都适用于 GET
So, you have to use GET in your case, not POST.
Security matters are irrelevant here.
所以,你必须在你的情况下使用 GET,而不是 POST。
安全问题在这里无关紧要。
回答by satdev86
Add an click event listener and send the request to another page.
添加单击事件侦听器并将请求发送到另一个页面。
回答by Obewan
You will need to wrap your "selections" in a form and use a button like so..
您需要将您的“选择”包装在一个表单中并使用这样的按钮..
<style type="text/css">
/* just styling the overall menu */
#my-post-link ul{
list-style-type:none;
text-align:center;
background-color:cadetblue;
font-size:1.1em;
font-weight:600;
padding:0.45em;
line-height: 1.75em;
}
/* following line would allow you to create a horizontal menu */
#my-post-link ul li{display:inline;white-space:nowrap;}
#my-post-link button {
background-color: transparent; /* <-- removes the gray button */
border: none; /* <-- removes the border around the button */
color: white;
font-size: 1.15em;
font-weight: 600;
}
#my-post-link button:hover {
background-color: white;
color: cadetblue;
cursor: pointer; /* <-- makes the button text act like a link on hover */
}
</style>
<form id="myForm" name="myForm" method="post">
<div id="my-post-link"><ul>
<li><button name="select-me" type="submit" value="var" onclick=\"myForm.action='./page.php';return true;\">Click me!</li>
</ul></div>
</form>
This will POST the value "var" to page.php with the key "select-me", so you would read that variable with...
这将使用键“select-me”将值“var”发布到page.php,因此您可以使用...读取该变量
$variable = $_POST["select-me"];
You could also further style the button(s) with pseudo classes to make them behave even more like a regular text link, but I think you get the idea.
您还可以使用伪类进一步设置按钮样式,使它们的行为更像常规文本链接,但我认为您明白了。
If you want to see what variables are POSTing back to your page, you can drop this code in somewhere:
如果您想查看哪些变量正在 POST 回您的页面,您可以将此代码放在某处:
foreach ($_POST as $key => $value) {
$i++;
${$key} = $value;
echo "<!-- //[{$i}] {$key}:{$value} -->\n";
}
You can also use var_dump($_POST) to view the POSTed data
您还可以使用 var_dump($_POST) 查看 POSTed 数据
回答by caras
you may also
你也可以
$.get('./page.php?param=true&data=1', function(data){$('#content').append(data);});

