簡體   English   中英

jQuery帖子沒有傳遞參數

[英]jQuery post not passing parameters

我有一個問題,即將變量發布到php腳本並在沒有刷新頁面的情況下返回結果。 php腳本koord.php經過測試,工作正常。

這是我的js代碼( adresamjest 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);
    });
}

koord.php:

$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]['geo‌​metry']['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(...)

你知道POSTGET之間的區別嗎?

你能試試嗎

     $.post("koord.php", { puna_adresa: adresa, mjesto: mjesto }, function (result) {
        $('#coords').val(result);
    });

其他方式:

    $.post("koord.php", $( "#testform" ).serialize(), function (result) {
        $('#coords').val(result);
    });

參考: http//api.jquery.com/jQuery.post/

你可以嘗試這個

$.post( "koord.php", $( "#testform" ).serialize() );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM