1

I am making a tooltip for World of Warcraft items. wowhead.com has a similar plugin, but they are using JavaScript for the whole plugin. So, I want to make it so when I hover over a paragraph, the MySQL query in my script should change. And it should get a value from input hidden. So, my form looks like this:

<form action="functions/itemvar.php" method="POST">
<p class="name">Random Item Name</p>
<input type="hidden" id="entry1" name="entry" value="12345">
</form>

And my SQL Query looks like this:

SELECT * FROM item_template WHERE entry = 12345

So, The entry in MySQL query from PHP script should change when you hover over the paragraph tag. So the MySQL query gets the result from input hidden.

I do use variable for the entry I just wrote 12345 temporary.

IS4
  • 11,945
  • 2
  • 47
  • 86
Tim Levinsson
  • 597
  • 2
  • 7
  • 20
  • 1
    you should post your php code as well – thrau Apr 19 '15 at 20:21
  • You almost certainly don't want to run a query every time someone hovers over an element; the load from that will be tremendous and many of the queries will return a response after the user has moved on. – ssube Apr 20 '15 at 04:07
  • I just want it to work like this http://www.wowhead.com/items=2 i dont know if they run a query everytime someone hover over the item name or how it actually works. But that's what i wanna do. And how the function works is not important aslong as i can get the same thing as that. Wowhead share their tooltip javascript but the javascript only selects items from the live world of warcraft and i want to use this function for custom items. So items that doesnt exist on blizzards server. Because i have my own private server and i want to display my custom items – Tim Levinsson Apr 20 '15 at 05:53

3 Answers3

2

You can do this with jQuery and Ajax. Assign a custom class to each paragraph you want to bind the hover event. Then catch that class with jQuery, and execute an AJAX call on hover to a PHP script that provides the query result.

Snake
  • 106
  • 1
  • 10
1

What have you tried so far?
All you need is send AJAX request to php file on hover event, right?

  1. $.hover()
  2. $.get()

plus SQL Injection!

Community
  • 1
  • 1
Jafar Akhondali
  • 1,552
  • 1
  • 11
  • 25
  • Are you asking or telling? Answers should provide answers not ask questions, maybe this should have been a comment? Also if you are answering you should provide examples, where possible, to go along with the links. – Patrick Evans Apr 19 '15 at 20:30
  • Well, I don't know anything about jQuery or AJAX, I only know php, html, css and MySQL. So i don't even know where to begin with the jQuery or how i should use AJAX. I have been looking around on google but i cannot find anything that would work without remaking the whole jQuery and then i rather start from beginning – Tim Levinsson Apr 19 '15 at 20:31
0

It sounds like you need a few things to get this working; overall, you need a way of:

  • Detecting that the client has hovered over your item name
  • Getting the hidden value client-side (i.e. in the browser)
  • Sending the hidden value to the server
  • Dealing with the response (e.g. drawing the tooltip)

The html you've posted is pretty close to achieving that, but I'd guess that you will have multiple item links within one page. With that in mind, you could first simplify your markup like so:

<p class="name item-hover-link" data-item-id="12345">
    Barman's Shanker
</p>

Now your html contains all the necessary information. However, the version you've posted is also fine, it just contains alot of stuff you don't necessarily need to achieve the particular goal you stated.

Next, we'll use jQuery to detect if you hover over an item. Note: many web developers will tend use jQuery rather than raw javascript, especially for ajax-related stuff. I'm especially lazy, so I'm opting for it also.

Make sure this jQuery/javascript either comes after your html or is booted in an onLoad event; otherwise, it won't be able to find any ".item-hover-link" elements.

function drawTooltip(data) {
    // Google different tooltip drawing implementations
    // I'd be amazed if there isn't a 'WoW-style' tooltip
    // guide somewhere out there
}

$(".item-hover-link").hover(
    function() {
        // Mouse entering the link

        // $this refers to the <p> that was hovered over
        var $this = $(this);

        // Check if any previously-loaded item data has been stored
        // e.g. they have hovered over it before
        var loadedItemData = $this.data("loaded-item-data");

        if(loadedItemData !== undefined){
            // It's been previously loaded! Use that data to draw
            // a tooltip, no need for a server round-trip
            drawTooltip(loadedItemData);
        }
        else {
            // It hasn't been loaded before, you need to GET it
            // from the server

            // Here's where data-item-id="12345" comes in
            var id = $this.data("item-id");

            var request = "/functions/itemvar.php?id=" + id;

            $.get(request, function(data) {
                // Your php is generating the response, do whatever
                // with it here, I'll assume you didn't touch it.

                drawTooltip(data);

                // And cache it, to prevent another round trip if they
                // re-hover (care for memory leaks though)
                $this.data("loaded-item-data", data);
            });
        }

    },
    function() {
        // Mouse leaving the link, you should use this
        // area to remove any initialized stuff while doing
        // your round-trip (loading animations, get requests,
        // tooltips, etc)
        hideTooltip();
    }
);

This kind of approach should get you most of the way there. However, you'll need to make sure your back end complies with the request (I'm assuming it's a GET request for tooltip stubs?). Also, you'll need a method for drawing a custom tooltip, much like wowdb and thottbot (is that still trendy with the kids these days?). I'd reccomend something like jQueryUI tooltip if you want a quick solution or check out the various approaches to tool-tipping in google.

Adam Kewley
  • 1,224
  • 7
  • 16
  • Thank you, Very much appreciated ! However it doens't work to just copy/paste it so i guess i need to add something more to it. Like i said i have no experience with jQuery at all so i cannot see if anything is missing and i don't really know how i should find the missing part if something is missing. – Tim Levinsson Apr 19 '15 at 21:16
  • I guess it's better if i just show you my scripts... But the code is messed up but it did work when i used a form in the html and POST method http://pastebin.com/7qAhpvk8 <-- itemvar.php http://pastebin.com/Fiv4iww2 <-- tooltip.php I will clean up the code when i have something that works – Tim Levinsson Apr 19 '15 at 21:23
  • Hey Tim, no problem. The code I have posted is just a basic framework that you'll need to tailor for your specific needs. I quickly debugged it on `WebStorm` and it seems to hit the relevant breakpoints within the browser OK but you will need to tailor your `php` backend and implement a tooltip drawing function. For debugging purposes, you could just fill `drawTooltip` with an `alert(data);` while you work on your backend, if you dont have a `javascript` debugger (yet!) – Adam Kewley Apr 19 '15 at 21:23
  • Hey, I'm personally a `ruby on rails` developer, I've only done a small (few hundred, maybe) hours on `php`. Looking at `itemvar.php`, you'll probably want to change to `var request = "/functions/itemvar.php?entry=" + id` in the javascript. Your php is only looking for `entry` with the line `$_GET['entry']`. I skipped past the various switches etc to your echoed text. In my solution, I'm assuming that you're building a `json` response, not a `html` response. At the moment, your php code is echoing an entire page (`` tags included). Instead, you only really want to echo the data. – Adam Kewley Apr 19 '15 at 21:30
  • If you want stuff like dynamic `ajax`-based behaviours in your page then I'd reccomend looking up something like *"php json builder"* in google. Trying to parse HTML (or, more generally, xml) using client-side javascript is usually a bit of a pain by comparison. However, if you are adamaent about doing *that* then libraries like `jQuery` come with methods like `$.parseHTML`. You'd have to read into it though – Adam Kewley Apr 19 '15 at 21:34
  • Alright, Thanks. I will read about all that. and see if i could get this to work. But it sounds like it wont be as easy as i thought. – Tim Levinsson Apr 19 '15 at 21:45