1

I'm new here, but in my last searches, I found everything that I needed here, except this question.

I want create a dynamic tooltip with jquery or something else, but my question is, if I can do a mysql query when the user pass the mose over the text than I want, for example:

<input type="checkbox" name="COL[1]" value="pmelendez" id="pmelendez" onmouseover="javascript:function_to_make_query(this.id);"/>pmelendez

And, in the function I need something like this:

<script>
 function_to_make_query(idreq){
  var result=<?php
  $q="SELECT complete_name from mydatabase where uname=" + idreq;
  $result=mysql_query($q,$my_conection);
  ?>
 }

</script>

But I need show the result on a tooltip or something like that.

For your help, thank you so much.

Skull118
  • 13
  • 5
  • 7
    `$q="SELECT complete_name from mydatabase where uname=" + idreq;` -- this would NEVER work. That's not how you pass a JavaScript variable to PHP. See this question: [Why does the PHP (or other server side) code in my Javascript not work?](http://stackoverflow.com/questions/13840429/reference-why-does-the-php-or-other-server-side-code-in-my-javascript-not-wor) – Amal Murali Nov 06 '13 at 15:41
  • 3
    You would need to use AJAX, but every mouseover would hit your server with traffic. Better to use a clicking mechanism. – Sablefoste Nov 06 '13 at 15:43
  • It's an example my friend, for give an idea. – Skull118 Nov 06 '13 at 15:45
  • Why did you include the angularjs tag to this question? – TheHippo Nov 06 '13 at 15:51

1 Answers1

1

You can't mix javascript and php. You have to use $.ajax() like,

HTML

<input type="checkbox" name="COL[1]" value="pmelendez" id="pmelendez"/>pmelendez

SCRIPT

$('#pmelendez').on('mouseenter',function(){
    $.ajax({
       url:'tooltip.php',
       data:{id:this.id},
       success:function(response)
       {
          // use response for tooltip
       }
    });
});

PHP- tooltip.php

<?php
    $q="SELECT complete_name from mydatabase where uname=" + $_REQUEST['id'];
    $result=mysql_query($q,$my_conection);
    $row=mysql_fetch_array();
    echo $row['complete_name'];
?>

Mysql is deprecated so use Mysqli

Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106