0

Hi I have been trying to pass an array of objects with geolocation data from jquery to a php script to save to a database. been up all night trying to get this working so may be missing something small from lack of sleep.

the jquery objects stucture is the following

var testData = [];            

        var coords = {
        lat: 12.6544885,
        lng: 23.545665
        };

        var pos = {
         timestamp: 1222355465,
          latlng: coords
            };

            testData.push(pos);


            var coords = {
        lat: 55.6544885,
        lng: 55.545665
        };

        var pos = {
         timestamp: 555,
          latlng: coords
            };

            testData.push(pos);

I am trying to post this via .ajax using the following

$.ajax({
            type: 'POST',
            data: JSON.stringify(testData),
            //change the url for your project
            url: 'www.mydomain.com/save2.php',
            success: function(data){
                console.log(data);
                alert('Sucess');
            },
            error: function(){
                console.log(data);
                alert('Error');
            }
        });

and I am decoding at the php side and attempting to place in a database using the following.

$myData = json_decode($_REQUEST['testData']);

$sql = "INSERT INTO walk (timestamp, latitude, longitude) ";
$sql .= "VALUES ($myData->timestamp, $myData->latlng->lat, $myData->latlng->lng)";

I would appriciate any input on this issue thanks.

10
  • 1
    Please run a print_r($myData) and post it. Commented Apr 13, 2013 at 14:28
  • +1 for value of my data, and also any error messages Commented Apr 13, 2013 at 14:31
  • have you tried $myData = json_decode($_POST['testData']) ? Commented Apr 13, 2013 at 14:32
  • Are you not using pdo or mysqli? Why insert those variables inside sql string? Use prepared statements. Commented Apr 13, 2013 at 14:37
  • @AxelA.Grazx I am quote new to jquery and php most of my proggramming experiance is console based. have to do this for a uni project. Could you tell me how I can run print_r and get an output as this script does not load the php page in the browser so would have to be reutned to the jquery script somehow thanks Commented Apr 13, 2013 at 15:11

1 Answer 1

3

In ajax options, try changing data: JSON.stringify(testData), to

data: { 'testData': JSON.stringify(testData) },

otherwise you won't have a valid query string.

Sign up to request clarification or add additional context in comments.

2 Comments

Beat me to it by less than a minute.
or better yet, get rid of the stringify call altogether. jquery will automatically convert the object into a query string, encoding it as json is unnecessary.

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.