0

how to make a hyperlink to download the upload file and by the same time it updates its database?

For now I can download the file but it doesn't change the file status.

Here is my code

<a href="reports/<?php echo $row['file']; ?>" target="_blank"  onclick="return runMyFunction();">Docx</a> 

<script type="text/javascript">
    Cufon.now(); 
    function runMyFunction() {
        <?php   
             mysql_query("update report_upload set status=1 from report_upload where year='$year' AND area='$area'",$conn);    
        ?>
        return true;
    }   
</script>

try1 .html

<form id='login' action='adminReportDownload.php' method='POST' enctype='multipart/form-data'>
                                                            <input type="hidden" value="<?php echo $area;?>" name="yeara">
                                                            <input type="hidden" value="<?php echo $year; ?>" name="yeary">
                                                            <input type="hidden" value="<?php echo $abc123; ?>" name="abc123">
                                                            <input type="submit" name="submit" id="submit" value="Download">
                                                            </form>

.php

$a = $_POST['yeara'];
$b = $_POST['yeary'];
$c = $_POST['abc123'];
$file_path = 'reports/'.$c;
$sqlABC = "UPDATE report_upload SET status='1', usedBy='$sesId' where year='$b' AND area='$a' ";
$eviABC = mysql_query( $sqlABC, $conn );

header("location: reports/$c");

2 Answers2

0

Because PHP is server-side, it is executed before your browser displays your web page. Thus, your JavaScript function runMyFunction contains nothing but a return true; statement.

Especially since trying to do server-side operations from the client-side is a bad idea, a better solution may be to have the link point to a PHP page that sends the file and updates the database at the same time.

For further reading, this question about PHP readfile may be helpful.

Hatchet
  • 5,320
  • 1
  • 30
  • 42
0

you can achieve this by send post request to server when click on the link.

  1. on the eventhandler, prevent the default behavior by e.preventDefault(); e.stopPropagation()
  2. write an ajax method to post the request to sever
  3. on serverside, update the database, write the file to frontend.
Sean
  • 2,990
  • 1
  • 21
  • 31