Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've gone as far as I can with the below script, but I can't for the life of me work out why the page id is not being sent to the PHP script.

Currently I'm grabbing the "index.php?id=12" with a PHP _GET command then inserting that into a Javascript variable. I set up a 2nd variable that includes that, as well as a string for latitude and longitude. When I test that the string is created via an alert, the string looks exactly like it should.

Inside my PHP script, I only every get the longitude and latitude data, but never the id data. Having read a number of other threads on here I believe the Ajax is expecting an object and that the string is unexpected. Unfortunately I do not know how to rectify this.

My Javascript is as follows:

    function GeoSuccess(position) {
    var id = <?php echo $id ?>; 
    var dataString = '?id='+id+'&action=geolocation&latitude='+position.coords.latitude+'&longitude='+position.coords.longitude; 

    $.ajax({
        url: 'search.php',
        type: 'GET',
        data: dataString,
        success: function (msg) {
        $('div#search-results').html(msg);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown)
        {   
        alert('Error submitting request.'); 
        }

    });
}
share|improve this question
add comment

5 Answers

up vote 1 down vote accepted

If you want an object rather than a string, this would work:

var data = { 
    id: id,
    action: 'geolocation',
    latitude: position.coords.latitude,
    longitude: position.coords.longitude,
}

$.ajax({
    url: 'search.php',
    type: 'GET',
    data: data,
// .... snip

I'm not sure but i don't think that '?' is expected in your dataString. Removing it could work too.

share|improve this answer
 
Works a charm. Thanks! –  Ashkas Mar 5 '12 at 10:11
add comment

use

 'id='+id+....

instead of

'?id='+id+....
share|improve this answer
add comment

Try to assign an object to ajax data parameter

data: {
    id : <?php echo $id; ?>,
    action : "",
    latitude : position.coords.latitude,
    longitude : position.coords.longitude
}
share|improve this answer
add comment
function GeoSuccess(position) {
    var id = <?php echo $id ?>; 
    var dataObject = new Object();
    dataObject.id=id;
    dataObject.action=geolocation;
    dataObject.altitude=position.coords.latitude;
    dataObject.longitude=position.coords.longitude;


    $.ajax({
        url: 'search.php',
        type: 'POST',
        data: dataObject ,
        success: function (msg) {
        $('div#search-results').html(msg);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown)
        {   
        alert('Error submitting request.'); 
        }

    });
}

try this..che

share|improve this answer
 
check the data sent in firebug net tab –  Rajesh Mar 5 '12 at 9:32
 
Unfortunately no success with this method. –  Ashkas Mar 5 '12 at 10:11
 
can you inspect the request header in firebug net tab?paste the content of request header here..Firebug is a mozilla firefox addon..paste the rquest header with url 'search.php' here..I am sure we will get some luck –  Rajesh Mar 5 '12 at 11:54
 
is this line var id = <?php echo $id ?>; evaluates id as null? –  Rajesh Mar 5 '12 at 12:10
add comment

You can add dataString to the url

url: 'search.php' + dataString,

or you can remove the leading '?' from the dataString

var dataString = 'id='+id+'&action=geolocation&latitude='+position.coords.latitude+'&longitude='+position.coords.longitude;

This way you will get the id.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.