weixin_33701294 2016-06-11 09:30 采纳率: 0%
浏览 5

ajax获取Excel表格php

I am tryinh to get excel form php through ajax call when i load url of specific php gives me the output but when the same php is called via ajax with some ajax value nothing shows up.. am not sure what to do

ajax:

var fromdate=  $("#fromdate").val();
var ToDate= $("#ToDate").val();
var Year=  $('#Year').val();
var Category=$('#Category_1').val();

                            $.ajax({
                                url: "http://localhost/demo.php",
                                type: "post",
                                data: {
                                    fromdate:fromdate,ToDate:ToDate,Year:Year,Category:Category
                                },
                                success: function(responsecon) {
                                      window.open('http://YOUR_URL','_blank' );


                                }

                            });

PHP:

<?php
    $conn=mysqli_connect('localhost','root','0000','xxxxx');

    $filename = "users_export"; 
    $fromdate = mysqli_real_escape_string($mysqli,trim($_POST["fromdate"]));
    $ToDate = mysqli_real_escape_string($mysqli,trim($_POST["ToDate"]));
    $Year = mysqli_real_escape_string($mysqli,trim($_POST["Year"]));
    $Category = mysqli_real_escape_string($mysqli,trim($_POST["Category"]));
    $sql = "Select * from xxxxxxxxxxx where category='$Category'";
    $result = mysqli_query($conn,$sql) or die("Couldn't execute query:<br>" . mysqli_error(). "<br>" . mysqli_errno()); 
    $file_ending = "xls";
    header("Content-Type: application/xls");
    header("Content-Disposition: attachment; filename=$filename.xls");
    header("Pragma: no-cache"); 
    header("Expires: 0");
    $sep = "\t"; 
    $names = mysqli_fetch_fields($result) ;
    foreach($names as $name){

    }
    print ("dasd" . $sep."dasd1" . $sep);
    print("
");
    while($row = mysqli_fetch_row($result)) {
        $schema_insert = "";
        for($j=0; $j<mysqli_num_fields($result);$j++) {
            if(!isset($row[$j]))
            $schema_insert .= "NULL".$sep;
            elseif ($row[$j] != "")
            $schema_insert .= "$row[$j]".$sep;
            else
            $schema_insert .= "".$sep;
        }
        $schema_insert = str_replace($sep."$", "", $schema_insert);
        $schema_insert = preg_replace("/
|
|
|/", " ", $schema_insert);
        $schema_insert .= "\t";
        print(trim($schema_insert));
        print "
";
    }
?>
  • 写回答

1条回答 默认 最新

  • weixin_33709609 2016-06-11 13:23
    关注

    Basically there are two problems in your jQuery ajax request:

    Sice you are specifying a content-type in your PHP script you have to tell jQuery what to expect from your ajax request using dataType: application/xls in your ajax object.

    This is why you don't get any response, because it fails and you have not specified an error callback function to handle the error.

    Moreover, in case of success you have to print somehow the content returned from the PHP script with something like $("#selector").html(responsecon);

    Here is the updated ajax request:

    $.ajax({
        url: "http://localhost/demo.php",
        type: "post",
        dataType: "application/xls", // Tell what content-type to expect from server
        data: {
            fromdate:fromdate,
            ToDate:ToDate,
            Year:Year,
            Category:Category
            },
        success: function(responsecon) {
            window.open('http://YOUR_URL','_blank' );
            $(#"some-container").html(responsecon); // Print the content
            },
        error: function() {
            alert("error");
            }
        });
    
    评论

报告相同问题?