【问题标题】:passing coordinates from view to controller through post method通过 post 方法将坐标从视图传递到控制器
【发布时间】:2012-02-20 19:59:13
【问题描述】:

我正在尝试将坐标值从视图传递到控制器 add,从地理定位 google api 获取,但我遇到了一些麻烦。这是我在名为 _add 的视图中使用的脚本。脚本可以正常工作,因为 javascript 控制台打印出坐标很好。

 <script type="text/javascript">

        $(document).ready(function(){
            get_location();
        });

        function get_location()
        {
            if(navigator.geolocation)
            {
                navigator.geolocation.getCurrentPosition(function(position){

                        var latitude = position.coords.latitude;
                        var longitude = position.coords.longitude;

                        $.post('http://firstaid:8888/add', { latitude:latitude, longitude:longitude });

                });
            }
        }

    </script>

现在我将把值从这个视图传递给控制器​​,这样我可以在 php codeigniter 控制器中用作变量。为此,我使用$this-&gt;input-&gt;post

function index()
{
    $data['title'] = 'Add';

    $latitude = $this->input->post('latitude');

    $this->load->view('templates/_header', $data);
    $this->load->view('_add');

}

但是当我打印出$latitude 时,变量是空白的。可能是哪里出错了?也许是因为我在加载视图之前调用了 post?

是否存在将数据传递给控制器​​的另一种方式?

【问题讨论】:

    标签: php javascript codeigniter


    【解决方案1】:

    你需要改变这一行:

    $.post('http://firstaid:8888/add', { latitude:latitude, longitude:longitude });
    

    到这里:

    $.post('http://firstaid:8888/add', 'latitude='+latitude+'&longitude='+longitude);
    

    当然,给猫剥皮的方法不止一种,因为我相信有一个 jQuery 方法可以将 JSON 序列化为正确的 POST 变量字符串格式,但我这里的方法肯定会起作用。

    【讨论】:

    • 我试过 print_r($_POST('latitude'));但我收到此错误:致命错误:函数名称必须是字符串?
    • $_POST 是一个数组,而不是一个函数。因此,要访问数组的成员,请使用方括号 [],例如:print_r($_POST['latitude']);
    • 我更正了......我犯了什么愚蠢的错误......无论如何我都更正了它,但我总是收到错误,它是空白的变量......
    • 只是为了涵盖所有基础,您是否尝试将警报或 console.log 调用放入您的 javascript 代码以确保 position.coords.latitude 的值实际上不是空白?
    【解决方案2】:

    您没有将 $latitude 传递给视图,因此您无法访问它。

    如果您正在加载新页面,则 $latitude 需要位于传递给视图的 $data 数组中。

    但是,您使用的是 Ajax,因此无法以这种方式加载新视图。您需要执行以下操作:

    $latitude = $this->input->post('latitude');
    echo json_encode( $latitude );
    

    然后在您的 Javascript 中处理响应。

    或者直接在控制器中使用 print_r 来确认变量是否存在。

    print_r($this->input->post('latitude'));
    

    在你的 Javascript 中试试这个:

    $.ajax({
         type: "POST",
         url: "/add",
         data: "latitude=" + latitude,
         dataType: "json",
         success: function(msg){
             alert('Completed');
         } //End success condition
     }); //End ajax
    

    在 Chrome 或 Firebug 中发出 Ajax 请求时,请确保“网络”面板已打开,以便您确认它正在找到您的控制器。

    【讨论】:

    • 先试试这个:print_r($this->input->post('latitude'));
    • 我更新了我的帖子。试用该 Ajax 功能,并通过“网络”面板检查该请求以确保该请求正在通过。
    • 我试过但没有得到结果...我将 .ajax 放在 $.post 的位置,但我收到 Uncaught SyntaxError: Unexpected end of input
    【解决方案3】:

    控制器文件:

    $this->load->library('googlemaps');
    
    $config['center'] = '37.4419, -122.1419';
    $config['zoom'] = 'auto';
    $this->googlemaps->initialize($config);
    
    $marker = array();
    $marker['position'] = '37.429, -122.1419';
    $marker['draggable'] = true;
    $marker['ondragend'] = 'updateDatabase(event.latLng.lat(), event.latLng.lng());';
    $this->googlemaps->add_marker($marker);
    $data['map'] = $this->googlemaps->create_map();
    
    $this->load->view('view_file', $data);
    

    查看文件:

    <html>
    <head>
        <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            function updateDatabase(newLat, newLng)
            {
                // make an ajax request to a PHP file
                // on our site that will update the database
                // pass in our lat/lng as parameters
                $.post(
                    "/my_controller/update_coords", 
                    { 'newLat': newLat, 'newLng': newLng, 'var1': 'value1' }
                )
                .done(function(data) {
                    alert("Database updated");
                });
            }
        </script>
        <?php echo $map['js']; ?>
    </head>
    <body><?php echo $map['html']; ?></body>
    </html>
    

    引用者:http://biostall.com/demos/google-maps-v3-api-codeigniter-library/updatedatabase

    【讨论】:

      猜你喜欢
      • 2015-12-23
      • 1970-01-01
      • 2015-04-28
      • 1970-01-01
      • 2016-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多