Jump to content

[SOLVED] Calling PHP / MySQL inside a javascript function


Andy-H

Recommended Posts

I am trying to make a "who's viewing topic" feature for a forum using this code:

 

<script type="text/javascript">
<!--
function loadingUp()
{
<?php

mysql_query("UPDATE `players` SET `pageat` = '$topicid' WHERE `playername` = '$player' LIMIT 1")or die(mysql_error());

?>
}

function loadingDown()
{
<?php

mysql_query("UPDATE `players` SET `pageat` = '' WHERE `playername` = '$player' LIMIT 1")or die(mysql_error());

?>
}
//-->
</script>

<body onLoad="loadingUp();" onUnLoad="loadingDown();">

 

However instead of processing the PHP on the page load/unload it just reads through it and updates the field to the topic id, then updates it back to blank as the script loads, is there a way I can do this?

I fixed it without AJAX as I cant see the point in learning it just for this.

 

I figured since I have two files that are required in every single file at the top I just added:

mysql_query("UPDATE `players` SET `pageat` = '' WHERE `playername` = '$player' LIMIT 1")or die(mysql_error());

 

In my require files and added:

mysql_query("UPDATE `players` SET `pageat` = '$topicid' WHERE `playername` = '$player' LIMIT 1")or die(mysql_error());

 

After the file require in my viewtopic.php

 

and:

 

<tr>
<td colspan=2><div align="center">Users Viewing This Topic:<br>
      <br>
<?php
$lol = mysql_query("SELECT * FROM `players` WHERE `pageat` = '$topicid' ORDER BY `rank` DESC")or die(mysql_error());
while($ass = mysql_fetch_assoc($lol)){

$viewer = $ass['playername'];

echo "<a href=\"profile.php?viewuser=".$viewer."\">".$viewer."</a> , ";

}
?>
</div></td>
</tr>

 

To show whose viewing the topic.

 

Thanks for the help guys. :D

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.