dongyan2267 2013-10-26 11:07
浏览 25
已采纳

无法看到ajax调用的结果

im trying to store an ajax respone to the Post php variable and then echo it on the same page, but sadly, it returns "Notice: Undefined index"

I think its something with the success part of the ajax.Could you help me correct it?

Thanks in advance:)

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Title</title>
</head>
    <form action="" method="post">
    <input type="submit" name="ido" value="Click" /></td>
    </form>
    <script>

var x=document.getElementById("log");
function getLocation()
  {
  if (navigator.geolocation)
    {
    navigator.geolocation.getCurrentPosition(showPosition);
    }
  else{x.innerHTML="GPS szolgáltatás nem müködik ezen a böngészőn, kérlek értesítsd a rendszergazdát!";}
  }
function showPosition(position)
  {
    navigator.geolocation.getCurrentPosition(showPosition);
     var latitude = position.coords.latitude;
     var longitude = position.coords.longitude;
     $.ajax({
         url: "test.php", 
         type: 'POST', //I want a type as POST
         data: {'name' : latitude },
         success: function(response) {
        alert(response);
      }
      });
    }
</script>

<?php 
    if(isset($_POST["ido"])){
    echo "<script>getLocation();</script>";
    $name=$_POST["name"];
print_r($_POST);
    }
?>
</html>
  • 写回答

1条回答 默认 最新

  • dsfdsf8888 2013-10-26 11:15
    关注

    First, your DOCTYPE declaration is wrong. getCurrentPosition is a HTML5 feature.

    Besides that getCurrentPosition needs a callback. Also you don't need any php here before sending the ajax request.

    Try this:

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <script src="http://code.jquery.com/jquery-latest.min.js"></script>
            <meta charset="utf-8">
            <title>Title</title>
        </head>
        <body>
    
        <div id="log"></div>
    
        <form action="" method="post">
            <input type="submit" name="ido" value="Click" /></td>
        </form>
    
        <script>
        $(document).ready(function(){
            $('form').submit(function(e){
                e.preventDefault();         
    
                if (navigator.geolocation) {
                    navigator.geolocation.getCurrentPosition(ajaxCall);         
                }else{
                    $('#log').html("GPS szolgáltatás nem müködik ezen a böngészőn, kérlek értesítsd a rendszergazdát!");
                }
    
            });
    
            function ajaxCall(position){
                var latitude = position.coords.latitude;
                var longitude = position.coords.longitude;
    
                $.ajax({
                    url: "test.php", 
                    type: 'POST', //I want a type as POST
                    data: {'latitude' : latitude, 'longitude' : longitude },
                    success: function(response) {
                        $('#log').html(response);
                    }
                });
            }       
        });
        </script>
        </body>
    </html>
    

    Your test.php should look like this:

    <?php
        echo 'Latitude: '.$_POST['latitude'].'<br>';
        echo 'Latitude: '.$_POST['longitude'];
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用