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.'); 
        }

    });
}
link|improve this question
feedback

6 Answers

up vote 0 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.

link|improve this answer
Works a charm. Thanks! – Ashkas Mar 5 at 10:11
feedback

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.

link|improve this answer
feedback

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

link|improve this answer
feedback
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

link|improve this answer
check the data sent in firebug net tab – Rajesh Mar 5 at 9:32
Unfortunately no success with this method. – Ashkas Mar 5 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 at 11:54
is this line var id = <?php echo $id ?>; evaluates id as null? – Rajesh Mar 5 at 12:10
feedback

use

 'id='+id+....

instead of

'?id='+id+....
link|improve this answer
feedback

Try to assign an object to ajax data parameter

data: {
    id : <?php echo $id; ?>,
    action : "",
    latitude : position.coords.latitude,
    longitude : position.coords.longitude
}
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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