[英]jQuery post not passing parameters
我有一個問題,即將變量發布到php腳本並在沒有刷新頁面的情況下返回結果。 php腳本koord.php
經過測試,工作正常。
adresa
, mjest
o和coords
是文本輸入框): $(document).ready(function () {
$('#coord_click').click(function () {
provjera();
});
});
function provjera() {
var adresa = $('#adresa').val();
var mjesto = $('#mjesto').val();
var puna_adresa = adresa + " " + mjesto;
$.post("koord.php", { puna_adresa: puna_adresa },function (result) {
$('#coords').val(result);
});
}
$puna_adresa = $_GET['puna_adresa'];
function getCoordinates($address){
$address = str_replace(" ", "+", $address);
$url = "maps.google.com/maps/api/geocode/…";
$response = file_get_contents($url);
$json = json_decode($response,TRUE);
return ($json['results'][0]['geometry']['location']['lat'].",".$json['results'][0]['geometry']['location']['lng']);
}
echo getCoordinates($puna_adresa);
完整的源代碼在這里: http : //pastebin.com/u/bradetic
謝謝!
你真的需要使用Jquery AJAX,這是一個例子:
<script>
function your_function()
{
// collect data like this
var formData = jQuery("#your_form_id").serializeArray();
jQuery.ajax({
type: "POST",
url:"your_php_page.php",
data:formData,
dataType:'json',
beforeSend: function()
{
},
success: function(resp)
{
alert(resp);
},
complete: function()
{
},
error: function(e)
{
alert('Error: ' + e);
}
});
}
</script>
你的PHP腳本應該是這樣的:
$puna_adresa=$_POST['puna_adresa'];
function getCoordinates($address){
$address = str_replace(" ", "+", $address);
$url = "maps.google.com/maps/api/geocode/…;;
$response = file_get_contents($url);
return $response;
}
$response = getCoordinates($puna_adresa);
echo json_encode($response);
Jquery POST不是問題。
你正在做$.post(...)
,這意味着你需要通過$_POST
獲取koord.php
的參數,並且你正在使用$_GET
,你看到問題是對的嗎?
改變$_GET['puna_adresa'];
到$_POST['puna_adresa'];
要么
在客戶端更改$.post(...)
$.get(...)
$.post(...)
。
POST
和GET
之間的區別嗎? 你能試試嗎
$.post("koord.php", { puna_adresa: adresa, mjesto: mjesto }, function (result) {
$('#coords').val(result);
});
其他方式:
$.post("koord.php", $( "#testform" ).serialize(), function (result) {
$('#coords').val(result);
});
你可以嘗試這個
$.post( "koord.php", $( "#testform" ).serialize() );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.