简体   繁体   English

如何通过POST传递Javascript变量,然后重定向到新页面?

[英]How to pass a Javascript variable via POST , and redirect to a new page?

I have a JavaScript variable, and I want to send via POST to a PHP page and I want to display the new page. 我有一个JavaScript变量,我想通过POST发送到PHP页面,并且要显示新页面。 I tried this solution, but it seems not work. 我尝试了此解决方案,但似乎不起作用。 I have checked within Wireshark, and the variable (key and value) are sent correctly, but the page, doesn't show anything. 我在Wireshark中进行了检查,并且变量(键和值)已正确发送,但是页面没有显示任何内容。

$(document).ready(function() {
            $("#tbID tr").click(function() {
                $(this).addClass('selected').siblings().removeClass('selected');
                var value = $(this).find('td:nth-child(2)').html();

            });

        });
        function send(value) {
            $.post("newPhp.php", {
                key : value

            }).done(function(data) {
                location.href="newPhp.php";
            });
        }

The newPhp.php:` newPhp.php:

    if(!empty($_POST["key"])){
        $value  = $_POST['key'];`
//something with $value
}

So what is the problem with this code ? 那么这段代码是什么问题呢? Are there any simpler solution ? 有没有更简单的解决方案?

function send(value) {
    $.post( "newPhp.php",
        {key:value},
        function( data ) {
            location.href="newPhp.php";
        }
    );
}

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

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