I have a website with articles, reviews, and commentary. I am able to log hits to specific items, now I want to log when my Print button is clicked.
The user function "log c_print()" and php includes appear as the first objects in the <body> block. The page has two custom buttons; "Print Article" and "Go Back".
Two functions are called when the visitor uses the Print button. A JavaScript function prints the page, and the PHP function should log it. Details are in the code block below. $id_no identifies the article to be printed.
The problem....log_cprint() executes whether the Print button is clicked or not!! The db field is incremented even when the "Go Back" button is used, or when the browser window close button is used, implying that the function runs on its own!
I have used php functions before and I can't figure out what I'm doing wrong here. I hope somebody can see my mistake. More page context is availabe.
D Lowery
CODE:
<?php
function log_cprint(){
global $connection; //mySQL connected earlier
global $id_no;
if ($connection) {
$query = "UPDATE reviews SET prints = prints+1 WHERE id_no = '$id_no' ";
$result = mysql_query( $query) or die ( "Did not update reviews print: " . mysql_error( )) ;
}
}
?>
<div align="center">
<a href="javascript:printext();"><img src="../Banners/printstory.gif" border="1" alt="Print Review" onclick="<?php log_cprint(); ?>"></a>
<a href="javascript:window.close();"><img src="../Banners/goback.gif" border="1" alt="Return"></a><br>
</div>
Here's the JS function--defined in <head>
<script type="text/javascript" language="JavaScript">
<!-- Begin
function printext(text){
text=document;
print(text);
}
-->
</script>