Log in

View Full Version : # of clicks insert through OnCLick Jscript function



JShor
08-27-2007, 08:42 PM
Hi, I am trying to make a code (which is a mix of Jscript and PHP) where when a person clicks on a link, it inserts the amount of recorded clicks into mysql...the code looks like this:



<?php

$query = "INSERT INTO clicks (click, date) VALUES('$click', '$date') ");

echo "<script type='text/javascript'>";
echo "function clicks(){"
mysql_query( $query );
echo "}";
echo "</script>";

?>


And here is an example hyperlink code:


<a href="click.php?url=http://www.google.com" onClick="clicks()">Google.com</a>


But it isn't inserting the data into mysql. Why is this not working? :?

djr33
08-27-2007, 08:55 PM
Javascript doesn't use PHP.

PHP is run server side, and processed to plain text (including css, html, javascript, etc.), then output as that. It doesn't run while you are looking at the page.

PHP can't be used within a javascript function like that.

The way you could do that would be to run the server side script again.

For example, you could run that at the top of your page automatically, and have the link simply reload the page, running it each time.

Or, use AJAX, which is a way of running a PHP script by calling it with Javascript, basically loading the page in the background (to javascript, not to your browser). That's more complex, though.