2

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.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>
2
  • @icecub, First argument for getJSON is URL which is passed in above example.. Commented Jul 3, 2016 at 17:08
  • Your URL of page and passed in getJSON are different.. Commented Jul 3, 2016 at 17:09

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

0

That's because PHP compiles on the server while jQuery compiles on client. You kept both in the same file. So by the time ajax runs PHP has already been compiled 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.

1 Comment

Thank you for the quick response. Your advice will be helpful in later implementations.
0

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.

2 Comments

Thank you for the quick response. But php has to be there for further implementations. However this will be useful later..
Ok... With this, you could "play" a little with the result, organise, format, etc... Then send it to a PHP page. BTW, thank for making me discover usefull this link (freegeoip). ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.