javascript - ajax get Excel form php -
i tryinh excel form php through ajax call when load url of specific php gives me output when same php called via ajax ajax value nothing shows up.. not sure 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 * xxxxxxxxxxx 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 $name){ } print ("dasd" . $sep."dasd1" . $sep); print("\n"); 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("/\r\n|\n\r|\n|\r/", " ", $schema_insert); $schema_insert .= "\t"; print(trim($schema_insert)); print "\n"; } ?>
basically there 2 problems in jquery ajax request:
sice specifying content-type in php script have tell jquery expect ajax request using datatype: application/xls
in ajax object.
this why don't response, because fails , have not specified error
callback function handle error.
moreover, in case of success have print somehow content returned php script $("#selector").html(responsecon);
here updated ajax request:
$.ajax({ url: "http://localhost/demo.php", type: "post", datatype: "application/xls", // tell content-type expect server data: { fromdate:fromdate, todate:todate, year:year, category:category }, success: function(responsecon) { window.open('http://your_url','_blank' ); $(#"some-container").html(responsecon); // print content }, error: function() { alert("error"); } });
Comments
Post a Comment