Search code examples
phpjavascriptimagefunctionpositioning

Problems with javascript/php image positioning system


I am working on a project which involves moving products around a virtual living room, I have the following function

<a href="javascript:void(0)" onclick="sendxandy( <? echo $_SESSION['numberOfProducts']; ?> )">Save Positions of Products</a>

and then the function is as follows:

`
function sendxandy(productAmount)
{
       if (productAmount == 1)
       {
   location.href="homeview.php?x=" + dd.elements.image1.x + "&y=" + dd.elements.image1.y;
       }
       if (productAmount == 2)
       {
   location.href="homeview.php?x=" + dd.elements.image1.x + "&y=" + dd.elements.image1.y + 
   "&xtwo= " + dd.elements.image2.x + "&ytwo=" + dd.elements.image2.y;
       }
       if (productAmount == 3)
       {
   location.href="homeview.php?x=" + dd.elements.image1.x + "&y=" + dd.elements.image1.y + 
   "&xtwo= " + dd.elements.image2.x + "&ytwo=" + dd.elements.image2.y + 
   "&xthree= " + dd.elements.image3.x + "&ythree=" + dd.elements.image3.y;
       }
       if (productAmount == 4)
       {
   location.href="homeview.php?x=" + dd.elements.image1.x + "&y=" + dd.elements.image1.y + 
   "&xtwo= " + dd.elements.image2.x + "&ytwo=" + dd.elements.image2.y + 
   "&xthree= " + dd.elements.image3.x + "&ythree=" + dd.elements.image3.y + 
   "&xfour= " + dd.elements.image4.x + "&yfour=" + dd.elements.image4.y;
       }
       if (productAmount == 5)
       {
   location.href="homeview.php?x=" + dd.elements.image1.x + "&y=" + dd.elements.image1.y + 
   "&xtwo= " + dd.elements.image2.x + "&ytwo=" + dd.elements.image2.y + 
   "&xthree= " + dd.elements.image3.x + "&ythree=" + dd.elements.image3.y + 
   "&xfour= " + dd.elements.image4.x + "&yfour=" + dd.elements.image4.y +
   "&xfive= " + dd.elements.image5.x + "&yfive=" + dd.elements.image5.y;
}
`

and the function continues just like this up to image 10. so as you can see the coordinates of the image are saved in the URL so that I can access them in php, my next function is this

<a class="code" href="javascript:void(0);" onclick="moveProduct(<? echo $_SESSION['numberOfProducts']; ?>)">Move Images Back</a>

and inside here i have this (the moveTo variables are php variables for some reason this display can't print the code, also the moveTo is another function which is provided by a second script):

`function moveProduct(moveAmount)
{
    if (moveAmount == 1)
    {   
        if(window.dd && dd.elements) 
        {
        dd.elements.image1.moveTo(, );
        }
    }
    if (moveAmount == 2)
    {   
        if(window.dd && dd.elements) 
        {
        dd.elements.image1.moveTo(, );
        dd.elements.image2.moveTo(, );
        }
    }
    if (moveAmount == 3)
    {   
        if(window.dd && dd.elements) 
        {
        dd.elements.image1.moveTo(, );
        dd.elements.image2.moveTo(, );
        dd.elements.image3.moveTo(, );
        }
    }

` now i know my loop structure is god awful :) but please bear with me. whats happens is inside the moveProduct function, whatever the last if "moveAmount == " is, then images will only be relocated at that number. for example if I have the function set as above only three images will be remembered, not one or two, or four or five, but three only. i actually have ten items so i have the above functions set up for 10 items, and ONLY 10 images will be remembered. When i run the function moveProduct when theres not 10 items on the page, nothing happens at all like I'll load one image, move it, click save, everything looks fine but when i move it back to remembered coordinates nothing happens.

please help any advice would be appreciated


Solution

  • hey i figured it out, the php variables were returning blank values which made the js function fail entirely :)