Php 通过post方法将坐标从视图传递到控制器
我试图将坐标值从视图传递到控制器Php 通过post方法将坐标从视图传递到控制器,php,javascript,codeigniter,Php,Javascript,Codeigniter,我试图将坐标值从视图传递到控制器add,该值取自地理定位google api,但我遇到了一些问题。这就是我在名为\u add的视图中使用的脚本。这些脚本之所以有效,是因为javascript控制台打印输出协调良好 <script type="text/javascript"> $(document).ready(function(){ get_location(); }); function get_loc
add
,该值取自地理定位google api,但我遇到了一些问题。这就是我在名为\u 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>
但是当我打印出$latitude
时,变量是空的。错误在哪里?也许是因为我在加载视图之前打电话给post
是否存在将数据传递给控制器的另一种方式?您需要更改此行:
$.post('http://firstaid:8888/add', { latitude:latitude, longitude:longitude });
为此:
$.post('http://firstaid:8888/add', 'latitude='+latitude+'&longitude='+longitude);
当然,剥猫皮的方法不止一种,因为我相信有一种jQuery方法可以将JSON序列化为正确的POST变量字符串格式,但我这里的方法肯定会起作用。您没有将$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请求时,请确保“网络”面板已打开,以便确认它正在查找控制器。控制器文件:
$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>
函数更新数据库(newLat、newLng)
{
//对PHP文件发出ajax请求
//在我们的网站上,将更新数据库
//作为参数传入我们的lat/lng
美元邮政(
“/my_controller/update_coords”,
{'newLat':newLat'newLng':newLng'var1':'value1'}
)
.完成(功能(数据){
警报(“数据库更新”);
});
}
引用人:我试过打印($\u POST('latitude');但是我收到了这个错误:致命错误:函数名必须是一个字符串。有什么想法吗?
$\u POST
是一个数组,不是函数。因此,要访问数组的成员,可以使用方括号[]
,例如:print\r($\u POST['latitude'])代码>我更正了..我犯了什么愚蠢的错误…不管怎样,我更正了它,但我总是收到错误变量它是空的…只是为了涵盖所有基础,您是否尝试将警报或console.log调用放入您的javascript代码中以确保position.coords.latitude的值实际上不是空的?让我们先试试这个:print\r($this->input->post('latitude');我更新了我的帖子。尝试该Ajax函数,并通过网络面板检查它来确保请求通过。我尝试了,但没有得到任何结果…我将.Ajax放在$.post的位置,但我收到了未捕获的语法错误:输入意外结束
<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>