我怎么能$_POST信息从一个页面到另一个点击链接?没有表单或提交框,只是如果最终用户点击一个链接,它会在打开的链接中发布相关信息。
伪代码,但:
//On first page
<a href="./page.php" onclick="post" value="var">Click me!</a>
...
//On the page the above links to
$variable = $_POST["var"];
我考虑的是下面的东西,虽然不够漂亮,但有效。我希望它是post而不是get。
//On first page
<a href="./page.php?var=<?php echo $variable; ?>">Click me!</a>
...
//On second page
$variable = $_GET["var"];
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>
参考链接不知道为什么不这样做:
//在第一页
<a href="./page.php?var=value">Click me!</a>
…//在页面上,上面的链接到
$variable = $_GET['var'];
更新:
根据你上面的评论:
GET显示URL中的信息,而POST则是这样不是。我不希望最终用户只是用任何东西编辑URL他们所希望的价值。@popnoodles,当用户点击超链接时,它会跳转到一个新的页面。——riista
您尝试这样做是出于安全性的原因,因此方法不是使用GET或AJAX的好方法,都不安全,因为都可以被篡改。您需要重新考虑要保护的内容,以及如何检查提交的数据是否有效。
选择GET和POST很简单
- POST应该用来修改服务器的状态。
- 所有其他情况都是GET
所以,在这种情况下,你必须使用GET,而不是POST。
安全问题与此无关。
添加单击事件侦听器并将请求发送到另一个页面。
你需要在表单中包装你的"selections",并使用一个像这样的按钮…
<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>
这将通过键"select-me"将值"var" POST到page.php中,所以你可以用…
$variable = $_POST["select-me"];
你也可以用伪类进一步设计按钮的样式,使它们的行为更像一个普通的文本链接,但我想你已经明白了。
如果你想看到什么变量被post回你的页面,你可以把这段代码放到某个地方:
foreach ($_POST as $key => $value) {
$i++;
${$key} = $value;
echo "<!-- //[{$i}] {$key}:{$value} -->'n";
}
您也可以使用var_dump($_POST)来查看张贴的数据
你也可以
$.get('./page.php?param=true&data=1', function(data){$('#content').append(data);});
假设你想点击一个链接登出,你想通过post方法,这是更安全的。你能做到的。
<a class="nav-link" href="logout.php" data-method="post">Logout (John)</a>
只需在link属性中添加data-method="post"。