简体   繁体   English

通过post方法将坐标从视图传递到控制器

[英]passing coordinates from view to controller through post method

I'm trying to pass coordinates values from view to controller add taken from geolocation google api but I' having some trouble.well this is the script I'm using in the view named _add . 我正在尝试将视图中的坐标值传递给从地理位置google api获取的控制器add ,但遇到了一些_add 。这是我在名为_add的视图中使用的脚本。 the scripts works because javascript console print out coordinates well. 该脚本有效,因为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>

Now I would pass values from this views to controller in this way I can use as variable in php codeigniter controller. 现在,我将以这种方式将值从此视图传递给控制器​​,从而可以在php codeigniter控制器中用作变量。 To do this I use $this->input->post . 为此,我使用$this->input->post

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

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

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

}

But when I print out $latitude , the variable is blank.where could be is the mistake? 但是当我打印出$latitude ,该变量为blank。哪里可能是错误的呢? maybe because I call post before loading views? 也许是因为我在加载视图之前先发帖吗?

Does it exist another way to pass data to the controller? 是否存在将数据传递到控制器的另一种方法?

controller file : 控制器文件:

$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);

view file : 查看文件 :

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

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

You need to change this line: 您需要更改此行:

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

to this: 对此:

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

There's more than one way to skin a cat, of course, as I believe there is a jQuery method that can serialize JSON to proper POST variable string format, but what I have here will certainly work. 当然,有多种方法可以使猫变皮,因为我相信有一种jQuery方法可以将JSON序列化为适当的POST变量字符串格式,但是我在这里肯定会行得通。

You're not passing $latitude to the view, so you cannot access it. 您没有将$ latitude传递给视图,因此无法访问它。

If you were loading a new page, $latitude would need to be in the $data array that is passed to the view. 如果要加载新页面,则$ latitude必须位于传递给视图的$ data数组中。

However, you're using Ajax, so you cannot load a new view that way. 但是,由于您使用的是Ajax,因此无法以这种方式加载新视图。 You need to do something like: 您需要执行以下操作:

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

Then process the response in your Javascript. 然后在您的Javascript中处理响应。

Or just use print_r directly in the controller to confirm the variable is there. 或直接在控制器中直接使用print_r确认变量在那里。

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

Try this in your Javascript: 在您的Javascript中尝试一下:

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

When making the Ajax request in Chrome or Firebug, make sure the "Network" panel is open so you can confirm that it is finding your controller. 在Chrome或Firebug中提出Ajax请求时,请确保“网络”面板处于打开状态,以便您可以确认它正在找到您的控制器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM