JQuery AJAX call mousemove

I have an image created with gdimage, which has 40,000 5x5 blocks that communicate with different user profiles, and I want AJAX to go and extract this profile from the database when I hover over one of the databases, finding x and y coordinates when it moves around the image.

Then, when clicked, the information received by him receives a link to this user profile.

Here is what I got so far:

Javascript / jQuery:

<script type="text/javascript">

    jQuery.fn.elementlocation = function() {

        var curleft = 0;
        var curtop = 0;

        var obj = this;

        do {

        curleft += obj.attr('offsetLeft');
        curtop += obj.attr('offsetTop');

        obj = obj.offsetParent();

        } while ( obj.attr('tagName') != 'BODY' );


            return ( {x:curleft, y:curtop} );

    };


    $(document).ready( function() {

        $("#gdimage").mousemove( function( eventObj ) {

            var location = $("#gdimage").elementlocation();
            var x = eventObj.pageX - location.x;
            var x_org = eventObj.pageX - location.x;
            var y = eventObj.pageY - location.y;
            var y_org = eventObj.pageY - location.y;

            x = x / 5;
            y = y / 5;

            x = (Math.floor( x ) + 1);
            y = (Math.floor( y ) + 1);

            if (y > 1) {

                block = (y * 200) - 200;
                block = block + x;

            } else {

                block = x;

            }

            $("#block").text( block );
            $("#x_coords").text( x );
            $("#y_coords").text( y );

                $.ajax({
                    type: "GET",
                    url: "fetch.php",
                    data: "x=" + x + "&y=" + y + "",
                    dataType: "json",
                    async: false,
                    success: function(data) {
                        $("#user_name_area").html(data.username);
                    }
                });

        });

    });

</script>

PHP:

<?

    require('connect.php');

    $mouse_x = $_GET['x'];
    $mouse_y = $_GET['y'];

    $grid_search = mysql_query("SELECT * FROM project WHERE project_x_cood = '$mouse_x' AND project_y_cood = '$mouse_y'") or die(mysql_error());

    $user_exists = mysql_num_rows($grid_search);

    if ($user_exists == 1) {

        $row_grid_search = mysql_fetch_array($grid_search);

        $user_id = $row_grid_search['project_user_id'];


        $get_user = mysql_query("SELECT * FROM users WHERE user_id = '$user_id'") or die(mysql_error());

        $row_get_user = mysql_fetch_array($get_user);

        $user_name = $row_get_user['user_name'];
        $user_online = $row_get_user['user_online'];

        $json['username'] = $user_name;
        echo json_encode($json);

    } else {

        $json['username'] = $blank;
        echo json_encode($json);

    }

?>

HTML

<div class="tip_trigger" style="cursor: pointer;">

    <img src="gd_image.php" width="1000" height="1000" id="gdimage" />

    <div id="hover" class="tip" style="text-align: left;">
        Block No. <span id="block"></span><br />
        X Co-ords: <span id="x_coords"></span><br />
        Y Co-ords: <span id="y_coords"></span><br />
        User: <span id="user_name_area">&nbsp;</span>
    </div>

</div>

Now the variables "block", "x_coords" and "y_coords" from the mousemove location work fine and appear in span tags, but they don’t get the PHP variables from the AJAX function, and I can’t understand why.

, , , , fetch.php, , ​​ "/user/view/? id = VAR_ID_NUMBER"

? - ?:)

+3
2

, , mousemove. . .

, , - .

PHP . PHP- JavaScript. PHP , HTML- . , ​​ .

fetch.php, JSON . { userid: 2837 }. , :

echo "{ userid: $user_id, username: $user_name }";

jQuery , (, ) JSON, JavaScript. , :

//data will contain a JavaScript object that was generate from the JSON
//string the fetch.php produce, *iff* it generated a properly formatted
//JSON string.
function(data) { 
  $("#user_id_area").html(data.user_id);
}

HTML- :

User ID: <span id="user_id_area">&nbsp;</span>

showHover - , .

mousemove:

jQuery.fn.elementlocation = function() {

    var curleft = 0;
    var curtop = 0;

    var obj = this;

    do {

    curleft += obj.attr('offsetLeft');
    curtop += obj.attr('offsetTop');

    obj = obj.offsetParent();

    } while ( obj.attr('tagName') != 'BODY' );


        return ( {x:curleft, y:curtop} );

};


$(document).ready( function() {

    var updatetimer = null;
    $("#gdimage").mousemove( function( eventObj ) {
        clearTimer(updatetimer);
        setTimeout(function() { update_hover(eventObj.pageX, eventObj.pageY); }, 500);
    }


    var update_hover = function(pageX, pageY) {
        var location = $("#gdimage").elementlocation();
        var x = pageX - location.x;
        var y = pageY - location.y;

        x = x / 5;
        y = y / 5;

        x = (Math.floor( x ) + 1);
        y = (Math.floor( y ) + 1);

        if (y > 1) {

            block = (y * 200) - 200;
            block = block + x;

        } else {

            block = x;

        }

        $("#block").text( block );
        $("#x_coords").text( x );
        $("#y_coords").text( y );

        $.ajax({
            type: "GET",
            url: "fetch.php",
            data: "x=" + x + "&y=" + y + "",
            dataType: "json",
            async: false,
            success: function(data) {
                //If you're using Chrome or Firefox+Firebug
                //Uncomment the following line to get debugging info
                //console.log("Name: "+data.username);
                $("#user_name_area").html(data.username);
            }
        });

    });

});
+3

PHP? json_encode ?

<div> <a> <div>, , jQuery .

, jQuery javascript, URL-, , HREF . , jQuery , .

:

CSS

#imagecontainer {
background-image: url('gd_image.php'); 
width: 1000px; 
height: 1000px;
position: relative;
}

#imagecontainer a {
height: 100px;
width: 100px;
position: absolute;
}

#block1 {
left: 0px;
top: 0px;
}

#block2 {
left: 100px;
top: 0px;
}

HTML

<div id="imagecontainer">
<a href="" id="block1"></a>
<a href="" id="block2"></a>
</div>

jQuery

$(document).ready(function(){
$("#block1").click(function(){ /* do what you need here */ });
});
0

Source: https://habr.com/ru/post/1789059/


All Articles