1

I have this script that determines the position of an element (x position, y position), I am trying to use ajax to send it to this update.php file and update the table.

this.addEventListener("load", doSomething, true);

function doSomething(e) {
    var myElement = document.querySelector("#myElement");     
    var position = getPosition(myElement);
    alert("The image is located at: " + position.x + ", " + position.y);
}

function getPosition(element) {
    var xPosition = 0;
    var yPosition = 0;

    while(element) {
        xPosition += (element.offsetLeft - element.scrollLeft + element.clientLeft);
        yPosition += (element.offsetTop - element.scrollTop + element.clientTop);
        element = element.offsetParent;
    }

    return { 
        x: xPosition,
        y: yPosition
    };
}

This gives me a nice alert with position, but just need it so in update.php I can retrieve it easily like $_POST['x'] and $_POST['y'].

War10ck
  • 12,387
  • 7
  • 41
  • 54
Joshua Olds
  • 177
  • 3
  • 11

1 Answers1

1

You can add the values to the "data" attribute in your AJAX call:

$.ajax({
    url: '/oneFolderFromRoot/update.php',
    type: 'POST',
    data: {'positionX': position.X, 'positionY': position.Y},
    success: function() {
    }
});

You can check for your POST data like:

if (isset($_POST['positionX'])){
    $positionX = $_POST['positionX'];
}
StaticVoid
  • 1,539
  • 10
  • 11