2

I am trying to call a PHP function using AJAX. Below is the script I used.

<script type="text/javascript" src="jquery.1.4.2.js">

    $(document).ready(function () { 

               // after EDIT according to 
               // @thecodeparadox answer

       $('#local').click(function(e){
            e.preventDefault();
            e.stopPropagation();
            promptdownload();
       });
    });

        function promptdownload(e) 
        {
        $.ajax({
             type: "POST",
             url: "js/prompt.php",
             data: { "get" : "runfunction", "action" : "promptlocal" },
             success: function (response) {
             }    
         });
        }
</script>

The corresponding PHP code (prompt.php) is:

<?php
$path1 = "downloads/1.jpg";
$browserFilename1 = "Local Travel";
$mimeType1 = "image/jpeg";


function promptToDownload($path, $browserFilename, $mimeType)
{

    if (!file_exists($path) || !is_readable($path)) {

        return null;
    }

    header("Content-Type: " . $mimeType);
    header("Content-Disposition: attachment; filename=\"$browserFilename\"");
    header('Expires: ' . gmdate('D, d M Y H:i:s', gmmktime() - 3600) . ' GMT');
    header("Content-Length: " . filesize($path));
    // If you wish you can add some code here to track or log the download

    // Special headers for IE 6
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    $fp = fopen($path, "r");
    fpassthru($fp);
}

if ($_POST["action"] = 'promptlocal')
{
    promptToDownload($_GET[$path1], $browserFilename1, $mimeType1);//comments
}

?>

This is how I code the button that is supposed to trigger the function:

<input type="button" id="local" name="local" value="Local Travel">

My expected output is to have this button promt the user: "where to save 1.jpg file".

However I couldn't make it work.

Any advise is highly appreciated.

Eli
  • 14,779
  • 5
  • 59
  • 77
rofans91
  • 2,970
  • 11
  • 45
  • 60

4 Answers4

6
$('local').click(function(e){

should be

$('#local').click(function(e){

As local is an id so you should use # before it. And also in your php code there are some missing quotes.

thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
  • Hi, Thx for the response. I fixed the missing parts according to your answer. However, it still won't work. Any suggestion? – rofans91 Jul 08 '12 at 16:13
  • @rofansmanao do you check your console? try with some `alert('message')` within you `success` function of `ajax` request. – thecodeparadox Jul 08 '12 at 16:16
1

Use Firebug(FF), Dragonfly(Opera), Developer Tools(Chrome). You can see all javascript errors, warnings and exceptions, and can see ajax requests data.

Andrey Vorobyev
  • 896
  • 1
  • 10
  • 37
0
data: { "get" : "runfunction", "action" : "promptlocal" }, 

Try to remove the quotes from "get" and "action".

Like this :

    data: { get : "runfunction", action : "promptlocal" }, 
Yanis
  • 1
0

It looks to me like you are trying to download a file with jquery/ajax. You will not get this to work with only ajax. This question has been answered several times on stackoverflow.

I hope this link will help you: Ajax File Download using Jquery, PHP

Community
  • 1
  • 1
Bas Wildeboer
  • 580
  • 6
  • 13