简体   繁体   English

jQuery帖子没有传递参数

[英]jQuery post not passing parameters

I have a problem with posting variable to php script and getting result back without refreshing page. 我有一个问题,即将变量发布到php脚本并在没有刷新页面的情况下返回结果。 php script koord.php is tested and it's working fine. php脚本koord.php经过测试,工作正常。

This is my js code ( adresa , mjest o and coords are text input boxes): 这是我的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: 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);   

Complete source code is here: http://pastebin.com/u/bradetic 完整的源代码在这里: http//pastebin.com/u/bradetic

Thank you! 谢谢!

You seriously need to use Jquery AJAX, here's an example: 你真的需要使用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>

And you PHP script should go like this: 你的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);

The Jquery POST is not the problem. Jquery POST不是问题。

Your are doing $.post(...) which means that you need to get the parameter in koord.php via $_POST , and you are using $_GET , you see the problem right? 你正在做$.post(...) ,这意味着你需要通过$_POST获取koord.php的参数,并且你正在使用$_GET ,你看到问题是对的吗?

Solution

Change $_GET['puna_adresa']; 改变$_GET['puna_adresa']; to $_POST['puna_adresa']; $_POST['puna_adresa'];

or 要么

change $.post(...) for $.get(...) in your client side. 在客户端更改$.post(...) $.get(...) $.post(...)

You know the difference between POST and GET right? 你知道POSTGET之间的区别吗?

Can you try this, 你能试试吗

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

Another way: 其他方式:

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

Ref: http://api.jquery.com/jQuery.post/ 参考: 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