0

I' ve a issue that I could not solve, I've read a lot of questions similar to my, but have not been able to solve.

This is my code:

Ajax call:

function download() {           
    var doctypeString = $('#doctypeComponent').attr('class')+"";
    var metaString = $('#metaComponent').attr('class')+"";
    var cssString = $('#cssComponent').attr('class')+"";
    var jsString = $('#jsComponent').attr('class')+"";
    var ajax = $.ajax({
        type: 'POST',
        url: 'php/download.php',
        data: {doctype : doctypeString, meta : metaString, css : cssString, js :  jsString},
        dataType: "text"
    });
                    
    ajax.done(function() {
        window.location = 'php/download.php';           
    });
    
    ajax.fail(function(jqXHR, error) {
        alert("Request failed: "+error);
    });
    
}

Php code is:

if(isset($_POST['doctype'])){
    $doctype = $_POST['doctype'];
    writeFile($doctype);
} else {
    $doctype = "error doctype\n";
    writeFile($doctype);
}

if(isset($_POST['meta'])){
    $meta = $_POST['meta'];
    writeFile($meta);
} else {
    $meta = "error meta\n";
    writeFile($meta);
}

if(isset($_POST['css'])){
    $css = $_POST['css'];
    writeFile($css);
} else {
    $css = "error css\n";
    writeFile($css);
}

if(isset($_POST['js'])){
    $js = $_POST['js'];
    writeFile($js);
} else {
    $js = "error js\n";
    writeFile($js);
}

$file = $file."\n\t</head>\n\t<body>\n\t</body>\n</html>";

.

downloadFile("test.html");

function writeFile($content) {
    global $file;
    $file = $file.$content."";
}

function downloadFile($filename) {
    global $file; 
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-disposition: attachment; filename="'.basename($filename).'";');
    header('Content-Length: '.strlen($file));
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Expires: 0');
    header('Pragma: public');
    echo $file;
    exit;   
}

Isset is always false, why?

I've tried also with GET instead of POST, but nothing changed.

Thanks.

Community
  • 1
  • 1
delvedor
  • 52
  • 1
  • 10

3 Answers3

3

You have ajax.done() going to download.php, this means when download.php get POST value from ajax, and it succeed, you didn't take it's response back but redirect to download.php, when it redirect to download.php it doesn't POST or GET anything, means it will always show isset() false, for "REDIRECTED" part. replace your window.location with alert() and I am sure you will get your values posted.

Sumit Gupta
  • 2,152
  • 4
  • 29
  • 46
  • @SumitGupta I was inspired by this: [link](http://stackoverflow.com/questions/20830309/download-file-using-an-ajax-request) – delvedor Apr 30 '14 at 08:57
  • Well, frankly you don't need to use Ajax for download, at most what you can do is give direct download link and than using jQuery just do the `window.location= $(this).attr("href"); e.preventDefault();` this will call your page and cancel the navigation, to pass your value simply pass them as Get. – Sumit Gupta Apr 30 '14 at 10:35
0

change datatype for ajax function

dataType: "html" or remove dataType option

doctype is a reserved keyword in javascript.

Write it as 'doctype' :

martian
  • 20
  • 5
0

This might not be very applicable to your case, but let me leave it here because the issue that was stressing me also landed me here.

In your Ajax request, make sure you havedataType: 'JSON', and in the PHP file, to send data over to the JSON request, add $response = "Hello World"; echo json_encode($response);

Denmau
  • 231
  • 2
  • 5