0

I have a script that rotates some images from their current position, there are 4 available rotations(positions) of the image and they are present by the values 0,1,2,3. My problem is that I need two variables from the database in my AJAX call to make this happen. I need the rotation and the src. My PHP currently look like this; Please notice the query where I take out rotation and src.

            <?php

            $item_number = -1; //This value is -1 in order to make the list start on 0
            $rowsize = 12;

            $itemArray = array();
            $finalArray = array();
            $results = 0;

                for ($i = 0; $i < $rowsize; $i++) {

                    $stmt = $mysqli->stmt_init();
                    $stmt->prepare('SELECT z, rotation, src, name FROM house_room1 INNER JOIN objects ON house_room1.object_id=objects.object_id WHERE house_room1.ref_id = ?'); 
                    $stmt->bind_param('i', $i
                    );
                    if ($stmt->execute()) {
                        $stmt->bind_result($z, $rotation, $src, $name);
                            while($stmt->fetch()) {

                               $results = 1;
                               $itemArray['number'] = $item_number;
                               $itemArray['name'] = $name;
                               $itemArray['ref_id'] = $z;
                               $itemArray['rotation'] = $rotation;
                               $itemArray['src'] = $src;
                               array_push($finalArray, $itemArray);

                            }
                            }
                            else {
                                echo 'Something went terribly wrong' . $mysqli->error;
                                    }
                            $stmt->close();

                            $item_number++;
                        }

                        if($results == 1){
                        aasort($finalArray,"ref_id");

                            foreach($finalArray as $arr){
                                echo '<li id="item-' . $arr['number'] . '" class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>' . $arr['name'] . ' 
                                <img class="rotate" id="img_'.$arr['number'].'" src="images/house/other/settings.jpg" onclick="rotateObject(this)" alt="'. $src.'">';
                            }
                        }

                        //create a function for sorting
                        function aasort (&$array, $key) {
                            $sorter=array();
                            $ret=array();
                            reset($array);
                            foreach ($array as $ii => $va) {
                                $sorter[$ii]=$va[$key];
                            }
                            asort($sorter);
                            foreach ($sorter as $ii => $va) {
                                $ret[$ii]=$array[$ii];
                            }
                            $array=$ret;
                        }
                        ?>

It then makes an AJAX call to this file:

function rotateObject(e)
{
    //e is handler which contains info about the item clicked. From that we can obtain the image id.
    //since the id are of the form img_123(some number), we need to extract only the number.
    var img_id = e.id.split("_")[1];
    var xmlhttp;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function(){
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            var getEle = document.getElementsByClassName('item' + img_id)[0];
            var imagePath ="images/house/objects/stone_chair_1.png"; //This has to be 
            getEle.src = imagePath + xmlhttp.responseText;
        }
    }
    xmlhttp.open("POST","database/update_settings_rotate.php",true);
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send("item_id="+encodeURIComponent(img_id));
}

This code is then supposed to have the imagePath equal to src. and then the rotation after it, but I don't know how I can get the src for that specific image from the database query made in the PHP file.

Any suggestions or advice on how I can pass that value? Thanks in advance.

1 Answer 1

1

One way to pass variables from php to javascript is:

<script>
    var myJavascriptVariable = <?php echo $myPhpVariable; ?>
</script>

Hope this help you

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

10 Comments

and, if the variable is a String, add quotes before <?php ..., and after ?>
I tried this, but I can't make it work somehow? When I do it, it just prints what it litterally says, like "<?php echo $blabla; ?>" instead of printing the actual value of $blabla
try this inside a .php file, this trick does'n work inside a .js or .html file.
Uhmm, no, you can't. BUT, you can put your script INSIDE a .php (within <script></script> tags) AND include this file with require() (<?php require('path_to_js/jsFile.php')). This is a dirty trick. Then you can use <? echo $myVar; ?>
@MauroCifuentes I can confirm to you and anybody else that this works even though ajax are asynchronous.
|

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.