Javascript laravel中的Jquery变量到php变量 if(navigator.geolocation){ navigator.geolocation.getCurrentPosition(函数(位置){ $.ajax({ 类型:“POST”, url:“/Get仓鼠”, 数据:{ x:位置坐标纬度, y:位置坐标经度 }, 成功:功能(数据){ 控制台日志(数据); 仓鼠=数据; } }); }); } @foreach($仓鼠为$仓鼠) {{HTML::image($hamster->foto,$hamster->titel,数组('width'=>200,'height'=>200))}
@endforeachJavascript laravel中的Jquery变量到php变量 if(navigator.geolocation){ navigator.geolocation.getCurrentPosition(函数(位置){ $.ajax({ 类型:“POST”, url:“/Get仓鼠”, 数据:{ x:位置坐标纬度, y:位置坐标经度 }, 成功:功能(数据){ 控制台日志(数据); 仓鼠=数据; } }); }); } @foreach($仓鼠为$仓鼠) {{HTML::image($hamster->foto,$hamster->titel,数组('width'=>200,'height'=>200))},javascript,php,jquery,ajax,laravel,Javascript,Php,Jquery,Ajax,Laravel,@endforeach 正如您所看到的,我从jquery结果中获取了数据,并希望它将其传递到php代码中的$hambers变量中。我该怎么做呢?既然我们需要弄清楚发生了什么,让我们回到基本点: 客户端请求获取/仓鼠 服务器创建并发送响应 客户端接收响应,解析页面,并在文档上运行任何javascript(或由其链接),并可以使用AJAX请求额外的资源 客户端通过AJAX使用x,y坐标发布/get仓鼠 服务器处理POST/get仓鼠请求并向客户端返回响应 你不能。php在服务器上执行,js在浏览器
正如您所看到的,我从jquery结果中获取了数据,并希望它将其传递到php代码中的
$hambers
变量中。我该怎么做呢?既然我们需要弄清楚发生了什么,让我们回到基本点:
获取/仓鼠
通过AJAX使用x,y坐标发布/get仓鼠
POST/get仓鼠
请求并向客户端返回响应你不能。php在服务器上执行,js在浏览器中执行。
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
$.ajax({
type: "POST",
url: "/gethamsters",
data: {
x: position.coords.latitude,
y: position.coords.longitude
},
success: function (data) {
console.log(data);
hamsters = data;
}
});
});
}
</script>
@foreach ($hamsters as $hamster)
<div class="col-sm-6 col-md-4 col-lg-3">
<div class="thumbnail">
<center><div>{{ HTML::image($hamster->foto, $hamster->titel, array( 'width' => 200, 'height' => 200 )) }}</div></center>
<div class="caption">
<h3><?php echo substr($hamster->titel, 0, 20); ?></h3>
<p><?php echo substr($hamster->beschrijving, 0, 50); ?></p>
<center><p><?php echo '<a href=/hamsterdetails/' . $hamster->id . ' class="btn btn-primary" role="button">Meer
info</a><br /> ';?></p></center>
</div>
</div>
</div>
@endforeach