View Full Version : Need some help with my script (hope this is the right place)

03-08-2006, 11:40 AM

I have the following (noob) question:

I have a DIV which I can drag. It's coordinates are stored in a file, so we know where the layer is.


<div id="koaladIi15v" align="center" style="position: absolute;">

The following script writes and gets the coordinates:

<script type="text/javascript">
var updateTimeout;
SET_DHTML(CURSOR_MOVE, "koaladIi15v");

if (window.XMLHttpRequest) { // Mozilla, Safari, ...
var xmlHttp = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");

updateTimeout = setTimeout("checkPos()", 1000);

function my_DropFunc() {
xPos = dd.obj.x;
yPos = dd.obj.y;

xmlHttp.open('GET', 'xml.php?xpos='+xPos+'&ypos='+yPos+'&random='+Math.random(), false);

updateTimeout = setTimeout("checkPos()", 1000);

function my_DragFunc() {

function checkPos() {
xmlHttp.open('GET', 'xml.php?random='+Math.random(), false);

response = xmlHttp.responseXML;
xPos = response.firstChild.getAttribute('xpos');
yPos = response.firstChild.getAttribute('ypos');

dd.elements.koaladIi15v.moveTo(xPos, yPos);
updateTimeout = setTimeout("checkPos()", 1000);
// -->
This is the xml file:
Header('Content-type: text/xml');

If ($_REQUEST['ypos'] && $_REQUEST['xpos']) {
$resXML = FOpen('position.xml', 'w');
FWrite($resXML, '<position xpos="'.$_REQUEST['xpos'].'" ypos="'.$_REQUEST['ypos'].'"/>');

$resXML = FOpen('position.xml', 'r');
$strXML = FGets($resXML, 4096);
Echo $strXML;
This is how the coordinates are written away:

<position xpos="429" ypos="271"/>
Now this works, but only with 1 DIV! I want to have more DIV's to be dragged, so I figured, add the DIV id in it. This way it nows what coordinates belong to what DIV (using the DIV id) Only HOW DO I DO THIS? Need some help here.

Also, if the id is already in the xml file, it must overwrite the old one and not put a new line onder it with the new coordinates. THis will make the xml file HUGE, don't want that:

DIV id 1 position xpos="429" ypos="271"
DIV id 2 position xpos="432" ypos="223"
DIV id 1 position xpos="422" ypos="233"

DIV id 3 position xpos="465" ypos="256"
DIV id 2 position xpos="429" ypos="271"
DIV id 5 position xpos="476" ypos="267"

If DIV id 3 is moved it will overwrite DIV id 3

Hope you guys can help me out!