Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3851524
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:10:34+00:00 2026-05-19T17:10:34+00:00

I have an image being created with gdimage, which has 40000 5×5 blocks linking

  • 0

I have an image being created with gdimage, which has 40000 5×5 blocks linking to different user profiles and I want that when you hover over one of those blocks, AJAX will go and fetch that profile from the database by detecting the x and y co-ords when it is moved over the image.

Then when it is clicked, with the information it has obtained link to that users profile.

Here is what I have 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 ‘block’, ‘x_coords’ and ‘y_coords’ variables from the mousemove location works fine and shows in the span tags, but it’s not getting the PHP variables from the AJAX function and I can’t understand why.

I also don’t know how to make it so when the mouse is clicked it takes the variables taken from fetch.php and directs the user to a page such as “/user/view/?id=VAR_ID_NUMBER”

Am I approaching this the wrong way, or doing it wrong? Can anyone help? 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T17:10:35+00:00Added an answer on May 19, 2026 at 5:10 pm

    Please see the comments about not doing a fetch with every mousemove. Bad bad bad idea. Use some throttling.

    That said, the problem is, you’re not using the result in any way in the success function.

    Your PHP function doesn’t return anything to the browser. PHP variables do not magically become available to your client-side JavaScript. PHP simply runs, produces an HTML page as output, and sends it to the browser. The browser then parses the information that was sent to it as appropriate.

    You need to modify your fetch.php to produce some properly formatted JSON string with the data you need. It would look something like { userid: 2837 }. For example, try:

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

    In your success callback, the first argument jQuery will pass to that function will be the result of parsing the (hopefully properly formatted) JSON result so that it becomes a proper JavaScript object. Then, in the success callback, you can use the result, in a way such as:

    //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);
    }
    

    Modify your HTML example as follows:

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

    Where showHover is a helper function that actually shows the hover.

    Here is a pattern for throttling the mousemove function:

    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);
                }
            });
    
        });
    
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an image on a webpage which is being dynamically generated by a
I have created an animated color wheel where the user navigates through different views
I have a small application in java which searches images using bing image search.
I have this Pin Model: class Pin < ActiveRecord::Base belongs_to :user belongs_to :image accepts_nested_attributes_for
Hey I have dynamic image begin created with imagecreate(), and I have random values
I've created a simple user control which is manually created with something like MyUserControl
I have an image created from the Google Maps API static map library and
I am designing a silverlight application in which i have a image control in
I have a particle system using basic spritebatch, where the particles are being created
I have a problem where users are reporting that their images aren't being uploaded

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.