php - 从数据库中检索值并显示在工具提示中

标签 php javascript jquery html css

我使用 javascript 创建了一个工具提示,并有一个使用该工具提示的 html 页面。 这是我的工具提示:

$(document).ready(function () {
//Tooltips
$(".tip_trigger").hover(function (e) {
    tip = $(this).find('.tip');
    var tipText = $(e.target).attr('ti');
    tip.html(tipText);
    tip.show(); //Show tooltip
}, function () {
    tip.hide(); //Hide tooltip        
}).mousemove(function (e) {
    var mousex = e.pageX + 20; //Get X coodrinates
    var mousey = e.pageY + 20; //Get Y coordinates
    var tipWidth = tip.width(); //Find width of tooltip
    var tipHeight = tip.height(); //Find height of tooltip

    //Distance of element from the right edge of viewport
    var tipVisX = $(window).width() - (mousex + tipWidth);
    //Distance of element from the bottom of viewport
    var tipVisY = $(window).height() - (mousey + tipHeight);

    if (tipVisX < 20) { //If tooltip exceeds the X coordinate of viewport
        mousex = e.pageX - tipWidth - 20;
    } if (tipVisY < 20) { //If tooltip exceeds the Y coordinate of viewport
        mousey = e.pageY - tipHeight - 20;
    }
    tip.css({ top: mousey, left: mousex });
});
});

这是 HTML:

<div class="container">
    <h1><span>Simple Example</span></h1>
<map name="Koala" class="tip_trigger">
<area ti="This is the left eye" shape="rect" coords="373,365,404,402" href="#" />
<area ti="Right Eye" shape="rect" coords="641,405,681,448" href="#" />

    <div class="tip"></div>

我希望数据库值显示“ti”所在的位置... 例如。 ti="数据库值" 如何才能做到这一点?我将使用 MySQL 和 php。我将非常感谢所有帮助。

最佳答案

在mysql中创建一个帮助表:

CREATE TABLE `help` (
  `helpID` varchar(100) NOT NULL,
  `helpText` text NOT NULL,
  UNIQUE KEY `helpID` (`helpID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

对于通用工具提示,在您需要帮助提示的页面上,放置以下 html:

<span id="productSKUHelp" class="helpTip questionMark"></span>

由于您希望在光标位于特定位置时显示工具提示,因此您应该能够使用 mousemove 函数并调用 showTip。

添加以下 JQuery:

  function showTip(thisTip){
    var postdata = {
      helpTip: thisTip.attr('id')
    };
    $.ajax({
             type    : "post",
             url     : "/getHelpRPC.php",
             dataType: "html",
             data    : postdata,
             success : function(data){
               if(data.length > 0){
                 var origContent = thisTip.html();
                 thisTip.removeClass("questionMark").addClass('helpTipBox');
                 thisTip.html(data);

               }else{
                 $('#helpTipBox').html("error");
               }
             },
             error   : function(xhr, textStatus, error){
               console.log(xhr.statusText);
               console.log(textStatus);
               console.log(error);
             }
           });

  }

  $(document).on('mouseenter', '.helpTip', function(){
    var thisTip = $(this);
    helpTipTimer = setTimeout(function(){
                                showTip(thisTip);
                              },
                              1000);
  })
  $(document).on('click', '.helpTip', function(){
    showTip($(this));
  })
  $(document).on('mouseleave', '.helpTip', function(){
    clearTimeout(helpTipTimer);
    $(this).html('').removeClass('helpTipBox').addClass('questionMark');
  });

添加以下 CSS:

.helpTip{
  padding-left:3px;
  z-index:900;
}

.questionMark:after{
  color:maroon;
  cursor:pointer;
  content:"?";
  top:2px;
  position:relative;
  padding:0 3px;
}

.helpTipBox{
  padding:5px;
  background-color:#97abcc;
  position:absolute;
  curson:text;
}

创建 getHelpRPC.php RPC:

<?php
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB);

if($mysqli->connect_errno){
    printf("Connect failed: %s %s %s\n", $mysqli->connect_error, __FILE__, __LINE__);
    exit();
}

$helpID = $_POST['helpTip'];
if($helpID != ""){
    $querystr = "SELECT helpText FROM help WHERE helpID ='" . $mysqli->real_escape_string($helpID) . "'";
    $res = $mysqli->query($querystr);
    $rowCount = $res->num_rows;
    if($rowCount > 0){
        $row = $res->fetch_object();
        echo $row->helpText;
    } else {
        echo "error selecting help.";
    }
}

现在您需要做的就是在 span 中创建一个唯一的 id,并将相应的记录添加到表中。

关于php - 从数据库中检索值并显示在工具提示中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14616786/

相关文章:

php - Wamp 同步问题

php - 如何获取另一个页面上bind_result()的值

javascript - 折叠 Bootstrap 导航栏无法打开

jquery - 如何在单击时为 div 设置动画,然后在第二次单击时反转动画?

javascript - 特定于每个页面的 jQuery Mobile 单独加载消息

php - Htaccess 重写匹配相同的模式

java - 正则表达式替换开始和结束之间的新行

javascript - 我想知道为什么2个变量不能进入js

java - 将 JavaBeans 源解析为 json 描述符

javascript - JQuery 点击事件不适用于带有 # 的链接 - 需要在移动设备上显示/隐藏子菜单