onmouseover to call php function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
timphp
Forum Newbie
Posts: 2
Joined: Tue Jan 17, 2012 3:50 am

onmouseover to call php function

Post by timphp »

Getting a bit desperate as I've tried a lot of things, so any help very appreicated!

The concept im struggling with works like this:
1.) There are graphics of people on a page. There is a textbox to the right of the screen which outputs data that corresponds to the tutor.
2.) What i want is that when a user rolls his mouse over the picture, the data in the textbox immediately updates, which is done via php
3.) The issue is thus calling a php function within onmouseover="" from a javascript command.
4.) The function only changes the values that should be displayed (but doesnt work anyway) - is there a way to test whether a php function is being called correctly? And do i need to include some kind of page refresher?

Thanks a lot for your help

******************************************

The code as I have it currently is this. Thanks! -Tim

...

Code: Select all

// Default setting to display a random persons data at page startup
<?


$username="removedforprivacy";
$password="removedforprivacy";
$database="tutors";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM tutors";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();


$i=rand(0,5);


$id=mysql_result($result,$i,"id");
$name=mysql_result($result,$i,"name");
$degree=mysql_result($result,$i,"degree");
$ib=mysql_result($result,$i,"ib");
$subjects=mysql_result($result,$i,"subjects");
$experience=mysql_result($result,$i,"experience");
$style=mysql_result($result,$i,"style");
$pic=mysql_result($result,$i,"pic");


?>



//This bit outputs all the graphics. Only the first immage calls the php updater function. $whichpic defines the flag for the person, which is passed to the updater function.
</p>
    <p class="bodytext"><a href="" onclick="" onmouseout="MM_swapImgRestore()" onmouseover="<? php updater($whichpic=1); ?>"><img src="../Tutors/Photos/tim2.gif" name="Image15" width="100" height="100" border="0" id="Image15" /></a><a href="max.html" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image16','','../Tutors/Photos/max1.gif',1)"><img src="../Tutors/Photos/max2.gif" alt="max" name="Image16" width="100" height="100" border="0" id="Image16" /></a><a href="nico.html" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image17','','../Tutors/Photos/nico1.gif',1)"><img src="../Tutors/Photos/nico2.gif" alt="nico" name="Image17" width="100" height="100" border="0" id="Image17" /></a><a href="sasha.html" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image18','','../Tutors/Photos/sasha1.gif',1)"><img src="../Tutors/Photos/sasha2.gif" alt="sasha" name="Image18" width="100" height="100" border="0" id="Image18" /></a>
 </p>




//Where the php data is output
<p class="sideheader">FEATURED TUTOR</p>
<p><img src="<? echo " $pic"; ?>" width="200" height="200"></p>
    <p><strong>Name: </strong> <? echo "$name"; ?></p>
    <p><strong>Degree: </strong> <? echo "$degree"; ?></p>
    <p><strong>IB: </strong> <? echo "$ib"; ?></p>
    <p><strong>Subjects Taught: </strong> <? echo "$subjects"; ?></p>
    <p><strong>Teaching Experience: </strong> <br /> <? echo "$experience"; ?>
     </p>
    <p><strong>Teaching Style:<br /> </strong><? echo "$style"; ?>
.</strong>    </p>
<p><!-- end .sidebar2 --></p>
</div>





// The updater function to call, which changes the information displayed
<?
php

function updater($whichpic)
{

$i = $whichpic;

$id=mysql_result($result,$i,"id");
$name=mysql_result($result,$i,"name");
$degree=mysql_result($result,$i,"degree");
$ib=mysql_result($result,$i,"ib");
$subjects=mysql_result($result,$i,"subjects");
$experience=mysql_result($result,$i,"experience");
$style=mysql_result($result,$i,"style");
$pic=mysql_result($result,$i,"pic");
}
?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: onmouseover to call php function

Post by Celauran »

What you really want here is an AJAX function to fire onmouseover.
timphp
Forum Newbie
Posts: 2
Joined: Tue Jan 17, 2012 3:50 am

Re: onmouseover to call php function

Post by timphp »

thanks - i dont have any experience with this, do you have any advice?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: onmouseover to call php function

Post by califdon »

Remember, PHP is a server side language; once the page has been sent to the browser, there is no PHP code left in the page and there is no server to interpret the PHP. Once a page is received by the browser, only a client side language, such as Javascript, can operate. Thus, ANY action by the user can only be detected and acted upon by Javascript. AJAX is a way to use a special built-in Javascript object called XMLHTTPRequest to send a request back to a PHP script on the server, to do whatever is needed and send back some data to be used by Javascript to update the page.

Depending on what it is you want to display when the user mouses over a graphic, you may not need to return to the server at all. If the amount of data isn't too much to do so, I would probably add it to the page initially, but use CSS to set the text element display property to "none" so that it is not visible. Then the onmouseover event can call Javascript function to change the display property to "inline" or something like that. If you're using jQuery, it's even easier, using the "hide" or "show" functions.

The details of any of these approaches are well covered in the many online Javascript tutorials. Just Google for javascript display and you should find lots of references.
Post Reply