0

I have a problem with posting variable to php script and getting result back without refreshing page. php script koord.php is tested and it's working fine.

This is my js code (adresa, mjesto and coords are text input boxes):

$(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);   

Complete source code is here: http://pastebin.com/u/bradetic

Thank you!

vsync
  • 118,978
  • 58
  • 307
  • 400
user2986503
  • 27
  • 1
  • 8
  • can you show the code of `koord.php`? – Tomas Ramirez Sarduy Nov 13 '13 at 07:06
  • $puna_adresa=$_GET['puna_adresa']; function getCoordinates($address){ $address = str_replace(" ", "+", $address); $url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=$address"; $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); – user2986503 Nov 13 '13 at 07:12
  • check my answer, next time add the code to the question ;) – Tomas Ramirez Sarduy Nov 13 '13 at 07:18

4 Answers4

1

You seriously need to use Jquery AJAX, here's an example:

<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:

$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);
Ali
  • 5,021
  • 4
  • 26
  • 45
  • Can I use instead jQuery("#your_form_id") jquery("#adresa,#mjesto") because i have multiple input fields an I need just these two. – user2986503 Nov 13 '13 at 07:18
  • 1
    Is the same thing... there is no problem with the client side code – Tomas Ramirez Sarduy Nov 13 '13 at 07:19
  • And instead alert(resp) can I put this $('#coords').val(resp) to show the results in input field? I haven't tried yet this because i'm at work now. – user2986503 Nov 13 '13 at 07:21
  • oops, my answer is perfect for this sort of problem but someone just vote down my answer, you are genius pal, i am still shocked :s – Ali Nov 13 '13 at 07:22
  • yes of coarse, your script will return you everything in resp array, you can easily put this array in a loop and alert it's contents. – Ali Nov 13 '13 at 07:30
  • @Ali: I downvoted the answer before because there was no problem with the ajax request, the problem was (as you pointed now) with getting the `puna_adresa` param via `$_GET`. Now is ok, downvote deleted ;) – Tomas Ramirez Sarduy Nov 13 '13 at 08:06
1

The Jquery POST is not the problem.

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?

Solution

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

or

change $.post(...) for $.get(...) in your client side.

You know the difference between POST and GET right?

Community
  • 1
  • 1
Tomas Ramirez Sarduy
  • 17,294
  • 8
  • 69
  • 85
0

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/

Krish R
  • 22,583
  • 7
  • 50
  • 59
0

you May try this

$.post( "koord.php", $( "#testform" ).serialize() );
Nikunj K.
  • 8,779
  • 4
  • 43
  • 53