简体   繁体   English

如何在同一页面中将 jQuery 变量发布到 PHP?

[英]How to POST jQuery variables to PHP in same page?

Both my jQuery code and PHP code are in the same PHP file (not two separate files).我的两个jQuery代码PHP代码在同一个PHP文件(而不是两个单独的文件)。

I want to POST jQuery Variable to the PHP code.我想将 jQuery 变量发布到 PHP 代码。

But it Shows some errors ("undefined index") when running the PHP file.但它在运行 PHP 文件时显示一些错误(“未定义索引”)。

PHP file is as follows (test.php). PHP文件如下(test.php)。

<?php 
    $country = $_POST['userCountry'];
    $ip = $_POST['userIp'];

    echo $country;
    echo $ip;
?>

<html>
<head><title></title>
    <script src = "jquery.min.js"></script>
    <script>
        $.getJSON("http://freegeoip.net/json/", function(data) {
            var country = data.country_name;
            var ip = data.ip;

            $.ajax({
                method:"POST",
                url:"test.php",
                data:{userCountry:country, userIp:ip}
            });
        });
    </script>
</head>
<body></body>
</html>
<?php 
if(!empty($_POST)){
    $country = $_POST['userCountry'];
    $ip = $_POST['userIp'];

    echo $country;
    echo $ip;
}
?>

<html>
<head><title></title>
    <script src = "jquery.min.js"></script>
    <script>
        $(document).ready(function(){
        $.getJSON("http://freegeoip.net/json/", function(data) {
            var country = data.country_name;
            var ip = data.ip;
            $.ajax({
                method:"POST",
                url:"test.php",
                data:{userCountry:country, userIp:ip},
                success:function(result){
                    $('body').html(result);
                }

            });
           });
        });
    </script>
</head>
<body></body>
</html>

Try this code.试试这个代码。 Just Tested OK刚测试好

I'm not sure this is the wanted result...我不确定这是想要的结果......
But it looks like it.但它看起来像。 So try this:所以试试这个:

(EDITED) (已编辑)

<html>
<head><title></title>
    <script src = "https://code.jquery.com/jquery-1.12.0.min.js"></script>

</head>
<body>
<div id="result1"></div>
<br>
<div id="result2"></div>

<script>
$(document).ready(function(){
    var country;
    var ip;

    $.getJSON("http://freegeoip.net/json/", function(data) {
        country = data.country_name;
        ip = data.ip;
        console.log(country+" "+ip);

        $("#result1").append(country);
        $("#result2").html(ip);
    });
});
</script>

</body>
</html>

Notice that there is no PHP or $.ajax... Only $getJSON.请注意,没有 PHP 或 $.ajax... 只有 $getJSON。

That's because PHP compiles on the server while jQuery compiles on client.那是因为 PHP 在服务器上编译,而 jQuery 在客户端编译。 You kept both in the same file.您将两者保存在同一个文件中。 So by the time ajax runs PHP has already been compiled thus giving you the message undefined .所以当 ajax 运行时,PHP 已经被编译,因此给你消息 undefined 。 Make a separate file for PHP part or use the isset() and refresh the page using jQuery once the data has been submitted.为 PHP 部分创建一个单独的文件或使用isset()并在提交数据后使用 jQuery 刷新页面。

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

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