简体   繁体   English

无法将JavaScript变量传递给php变量

[英]Can't pass a javascript variable to php variable

I tried all the stackoverflow posts about this, I spend two days and still no success 😔, I am trying to pass a JavaScript variable to a PHP variable, I tried this for example and still not succeed, it does not print noting on the screen, here is a full code: 我尝试了所有与此有关的stackoverflow帖子,我花了两天时间仍未成功😔,我试图将JavaScript变量传递给PHP变量,例如,我尝试了此操作但仍未成功,它没有在屏幕上显示,这是完整代码:

here cc.html : 在这里cc.html

<!DOCTYPE html>
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    </head>
    <body>

        <script>
            $(document).ready(function(){
                //treehouse code
                var $my_variable = "something";
                $.ajax({
                    url: 'cc.php',
                    data: {x: $my_variable},
                    type: 'POST'
                });
            });
        </script>
    </body>
</html>

and here cc.php : 和这里cc.php

<?php
if(isset($_POST['x'])){
    echo $_POST['x'];
}
?>

What should I do? 我该怎么办?

You don't do anything with the result of the AJAX call. 您不会对AJAX调用的结果执行任何操作。 Add a callback function: 添加一个回调函数:

<!DOCTYPE html>
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    </head>
    <body>

        <script>
            $(document).ready(function(){
                //treehouse code
                var $my_variable = "something";
                $.ajax({
                    url: 'cc.php',
                    data: {x: $my_variable},
                    type: 'POST'
                }).done(function (result) {
                    alert(result);  // <--- do something with the result
                });
            });
        </script>
    </body>
</html>

And your PHP code remains unchanged: 而且您的PHP代码保持不变:

<?php
if(isset($_POST['x'])){
    echo $_POST['x'];
}
?>

What you do in that callback function is up to you. 您在回调函数的最多就是你。 You can alert() the value, log it to the console, write it somewhere on the page, etc. 您可以alert()值,将其记录到控制台,将其写在页面上的某个位置,等等。

If you want to print it on the screen you should use success: function(data)$('#result').html(data)} here is code example: 如果要在屏幕上打印,则应使用success: function(data)$('#result').html(data)}这是代码示例:

            $(document).ready(function() {
                $('#sub').click(function() {
                    var $my_variable = "something";
                    $.ajax({
                        url: 'cc.php',
                        type: 'POST',
                        data: { x: $my_variable },
                        success: function(data) {
                            $('#result').html(data)
                        }
                    });
                });
            });

您也可以尝试$.post

$.post( "cc.php", { x: $my_variable} );

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

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