admin管理员组文章数量:1327744
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
3 Answers
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
版权声明:本文标题:javascript - How to POST jQuery variables to PHP in same page? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742234647a2437852.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论