0

I have written the code , so user can select "Client Name" in first dropdown and the Select "Tag" and click on Submit.

When the user click on Submit ,they will get a CSV file as output with the values which matches the two criteria selected.

I am unable to get the CSV as output. I think I have done something wrong while passing values. Please help.

The Code on Main Page is :

    <html>
<head>
</head><?php include "connection.php"; ?>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.js"></script>
<script>
function getTag(val)
{

    $.ajax({
        type: "POST",
        url: "get_tag.php",
        data: "ClientID="+val,
        success: function(data){

            $("#Tag_List").html(data);
        }

    });
}

function getCSV()
{
var e = document.getElementById("Client-list");
var strClient = e.options[e.selectedIndex].value;
var f = document.getElementById("Tag_List");
var strTag = f.options[f.selectedIndex].text;
$.ajax({
        type: "POST",
        url: "get_CSV.php",
        data: "ClientID="+strClient + "Tag="+strTag,
        success: function(){

            window.console.log('Successful');
            return false;
        }

    });
}
</script>
<body>
<form>
<label> Select Client Name </label>
<Select id="Client-list" onChange="getTag(this.value)">
<option value="">Select Client</option>
<?php 
$sql=" SELECT * FROM CLIENTDETAILS";
$result = $dbhandle->query($sql);
while ($rs=$result->fetch_assoc()){
?>
    <option value=" <?php echo $rs["ClientID"]; ?> "> <?php echo $rs["ClientName"] ?> </option>
    <?php
 }  ?>
</Select>
<label> Tag Name: </label>
<Select id="Tag_List">
<option value=""> Select Tag</option>
</Select>
<button value="submit" onClick="return getCSV();">Submit</button>
</form>
</body>
</head>
</html>

The Code on get_CSV.php is :

    <html>
<?php 

define("HOSTNAME","localhost");
define("USERNAME","root");
define("PASSWORD","");
define("DATABASE","mydb");

$con=new mysqli(HOSTNAME,USERNAME,PASSWORD,DATABASE) or die("Unable to connect");

$query="SELECT * FROM listdetails WHERE ClientID='".$_POST["ClientID"]."' AND Tag='".$_POST["Tag"]."';"

$result = mysqli_query($con, $query);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);

$fp = fopen('file.csv', 'w');

foreach ($row as $val) {
    fputcsv($fp, $val);
}

fclose($fp);


?>

</html>
John Conde
  • 207,509
  • 96
  • 428
  • 469
Deepak Tiwari
  • 124
  • 1
  • 2
  • 11

3 Answers3

1

You never send the csv file, you should do something like that at some point :

header('Content-Type: application/csv');
header('Content-Disposition: attachement; filename="filename.csv"');
echo file_get_contents('file.csv'); exit();
vincenth
  • 1,656
  • 3
  • 17
  • 26
0

I changed the code for the function which passe

function getCSV()
{
//alert("Working");
var e = document.getElementById("Client-list");
var ClientID = e.options[e.selectedIndex].value;
var f = document.getElementById("Tag_List");
var strTag = f.options[f.selectedIndex].text;
$.post('get_CSV2.php',{postname:ClientID , posttag:strTag},
insertusernamehere
  • 21,742
  • 7
  • 80
  • 113
Deepak Tiwari
  • 124
  • 1
  • 2
  • 11
0
    <html>
<?php 
include "connection.php";
$cli =$_POST['postname'];
$tli =$_POST['postage'];
//this could not be the best way to prevent SQL Injection , but it may help if you are new to, php like me:)
$cid= substr($cli, 0, strpos($cli, ';'));
$con = mysql_connect('localhost', 'root', '') or die (mysql_error());
$db  = mysql_select_db("mydb", $con);
$filename ='upload/'.strtotime("now").'.csv';
$fp= fopen($filename,"w");
$query="SELECT * FROM listdetails WHERE ClientID='".$cid."' AND  Tag='".$tli."'";
$rows = mysql_query($query);
while ($row = mysql_fetch_assoc($rows)) fputcsv($fp, $row);
?>
</html>
Deepak Tiwari
  • 124
  • 1
  • 2
  • 11