1

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

    });
}

5 Answers 5

3

use

 'id='+id+....

instead of

'?id='+id+....
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

Try to assign an object to ajax data parameter

data: {
    id : <?php echo $id; ?>,
    action : "",
    latitude : position.coords.latitude,
    longitude : position.coords.longitude
}

Comments

0
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

4 Comments

check the data sent in firebug net tab
Unfortunately no success with this method.
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
is this line var id = <?php echo $id ?>; evaluates id as null?
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.