PDA

View Full Version : Javascript and MySql



hemi519
12-26-2010, 11:41 PM
Hi All,

I have written a php script which allows users to download the song or image only once. But when the page is loaded without clicking on the link php is automatically executing in javascript function and setting the database field to 1. how to get rid of this. the following is my code

echo"
<a href='http://localhost/images/images.rar' onclick=data()> Please download this movie</a>";

<head>

<script type="text/javascript">
function data()
{

<?php
$res=mysql_query("update movie database set download=1 where id='$id'");
?>
}
</script>

Schmoopy
12-27-2010, 12:19 AM
You have to understand that JavaScript and PHP don't work the same way.

PHP is executed before any JavaScript, PHP doesn't look at JavaScript control statements like "if".

The thing you need for this to work is AJAX.

Take a look at the JQuery library, as this offers an easy way of sending AJAX requests and you can update your database this way.

To clarify:



<script type="text/javascript">
function data()
{

<?php
$res=mysql_query("update movie database set download=1 where id='$id'");
?>
}
</script>


PHP ignores everything that's not within the PHP tags in the above code, so all PHP sees is:



<?php
$res=mysql_query("update movie database set download=1 where id='$id'");
?>


PHP doesn't care about anything outside of the PHP tags, and JavaScript is completely meaningless to it.

hemi519
12-27-2010, 12:37 AM
is thr any other way to do this. I dont no how to use Jquery and ajax for this

Schmoopy
12-27-2010, 12:41 AM
It depends, does it matter if the page refreshes?

hemi519
12-27-2010, 12:44 AM
does't matter

Schmoopy
12-27-2010, 12:55 AM
Ok, you're going to have to change how the download process works.

Instead of linking directly to the file itself, you need to go through another file, which will then tell the browser to download the file.

So, change your current code to:



<?php
echo "<a href='download.php?file=http://localhost/images/images.rar&id={$id}'> Please download this movie</a>";
?>


Then make a new file called download.php:



<?php

if(isset($_GET['file'])) {

$file = $_GET['file'];
$id = mysql_real_escape_string($_GET['id']);

if(file_exists($file)) {

// Set headers
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: binary");

$update = mysql_query("update movie database set download=1 where id='$id'");


} else {

echo 'That file does not exist.';

}


}

?>


That should do it.

You should note that the user may click cancel when presented with the option of downloading the file, so the file download counter may not be 100% accurate.

P.S: Doing it this way will take the user to a blank page, where the file is downloaded. To avoid this happening, you can have the download.php page open in a popup. Let me know if you want to know how to do that.

hemi519
12-27-2010, 01:32 AM
thank u it is working fine now