Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

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

4 Answers

up vote 5 down vote accepted
$('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.

share|improve this answer
Hi, Thx for the response. I fixed the missing parts according to your answer. However, it still won't work. Any suggestion? – rofansmanao Jul 8 '12 at 16:13
@rofansmanao do you check your console? try with some alert('message') within you success function of ajax request. – thecodeparadox Jul 8 '12 at 16:16
Added and not working... – rofansmanao Jul 8 '12 at 16:19

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

share|improve this answer
data: { "get" : "runfunction", "action" : "promptlocal" }, 

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

Like this :

    data: { get : "runfunction", action : "promptlocal" }, 
share|improve this answer
It doesn't make sense. It has to be quoted as it has to follow the JSON convention! – rofansmanao Jul 9 '12 at 20:32

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.