PDA

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



Null
03-08-2006, 11:40 AM
Hi,

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:

<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);
xmlHttp.send(null);

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

function my_DragFunc() {
clearTimeout(updateTimeout);
}

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

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

dd.elements.koaladIi15v.moveTo(xPos, yPos);
updateTimeout = setTimeout("checkPos()", 1000);
}
// -->
</script>
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'].'"/>');
FClose($resXML);
}

$resXML = FOpen('position.xml', 'r');
$strXML = FGets($resXML, 4096);
Echo $strXML;
FClose($resXML);
?>
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:

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

BUT:
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!

Null