使用onclick发布变量? [英] Post variable with onclick?

查看:93
本文介绍了使用onclick发布变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过点击链接将$ _POST信息从一个页面转到另一个页面?没有表单或提交框,只要最终用户点击链接,它就会在打开的链接中发布相关信息。



伪代码,但是:

  //第一页
...
//在页面上面链接到
$ variable = $ _POST [var];

我考虑的是以下内容,虽然不够漂亮,

  //第一页
< a href =./ page.php?var =<?php echo $ variable;?>>点击我!< / a>
...
//第二页
$ variable = $ _GET [var];


试试这个:

 < a href =link.phpclass =post>使用链接提交内容< / a> 

< script type =text / javascript>
jQuery(document).ready(function($){
$(。post)。on(click,function(){
$ .ajax({
url:http://www.yourwebsite.com/page.php,
类型:POST,
data:{名称:John,位置:Boston},
成功:函数(响应){
//执行动作
},
错误:function(){
//执行动作
}
});
});
});
< / script>

参考链接

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.

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"];

解决方案

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>

Reference Link

这篇关于使用onclick发布变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆