Results 1 to 7 of 7

Thread: Javascript and MySql

  1. #1
    Join Date
    Nov 2010
    Posts
    115
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Javascript and MySql

    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>

  2. #2
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    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:

    Code:
    <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 Code:
    <?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.

  3. #3
    Join Date
    Nov 2010
    Posts
    115
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default

    is thr any other way to do this. I dont no how to use Jquery and ajax for this

  4. #4
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    It depends, does it matter if the page refreshes?

  5. #5
    Join Date
    Nov 2010
    Posts
    115
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default

    does't matter

  6. #6
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    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 Code:
    <?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 Code:
    <?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.
    Last edited by Schmoopy; 12-27-2010 at 01:11 AM. Reason: Added P.S

  7. The Following User Says Thank You to Schmoopy For This Useful Post:

    hemi519 (12-27-2010)

  8. #7
    Join Date
    Nov 2010
    Posts
    115
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default

    thank u it is working fine now

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •