admin管理员组

文章数量:1327883

Both my jQuery code and PHP code are in the same PHP file (not two separate files).

I want to POST jQuery Variable to the PHP code.

But it Shows some errors ("undefined index") when running the PHP file.

PHP file is as follows (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("/", 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>

Both my jQuery code and PHP code are in the same PHP file (not two separate files).

I want to POST jQuery Variable to the PHP code.

But it Shows some errors ("undefined index") when running the PHP file.

PHP file is as follows (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/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>
Share Improve this question edited Apr 19, 2020 at 6:28 mujuonly 11.8k5 gold badges48 silver badges80 bronze badges asked Jul 3, 2016 at 17:04 AnushkaMAnushkaM 631 gold badge2 silver badges10 bronze badges 2
  • @icecub, First argument for getJSON is URL which is passed in above example.. – Rayon Commented Jul 3, 2016 at 17:08
  • Your URL of page and passed in getJSON are different.. – Rayon Commented Jul 3, 2016 at 17:09
Add a ment  | 

3 Answers 3

Reset to default 7
<?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/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

That's because PHP piles on the server while jQuery piles on client. You kept both in the same file. So by the time ajax runs PHP has already been piled thus giving you the message undefined . Make a separate file for PHP part or use the isset() and refresh the page using jQuery once the data has been submitted.

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./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/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.

本文标签: javascriptHow to POST jQuery variables to PHP in same pageStack Overflow