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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.