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.

2
  • 2
    missing some quotes and minor syntax errors Commented Jul 8, 2012 at 15:54
  • 1
    if ($_POST["action"] = 'promptlocal'), this is an assignment and not a comparison, you need to use == to compare values! Commented Jul 8, 2012 at 22:12

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, Thx for the response. I fixed the missing parts according to your answer. However, it still won't work. Any suggestion?
@rofansmanao do you check your console? try with some alert('message') within you success function of ajax request.
1

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

Comments

0
data: { "get" : "runfunction", "action" : "promptlocal" }, 

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

Like this :

    data: { get : "runfunction", action : "promptlocal" }, 

1 Comment

It doesn't make sense. It has to be quoted as it has to follow the JSON convention!
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.