I am trying to attach an event listener to a button that will make an ajax call when clicked.
I have set aside some php in a file we shall call test.php. I have used the live jquery function to attack a click event to the button. The ajax function will pass a value of an input element on the page to the test.php. The test.php file will then use that value to determine the url of the file it is to use. The test.php file dedupes the csv file based on matching emails. The csv file is uploaded via an upload input element.
Here is my code, that is not working currently. Fiddler shows that a request is being sent out to the server but a response is not forthcoming.
HTML
<input type="button" id="csv_dedupe" value="dedupe-file">
Jquery
$_CFG_PROCESSORFILE = "http://localhost/Gene/IMEXporter/include/IMEXporter_processor.php";
$("#csv_dedupe").live("click", function(e) {
file_name = 'C:\\server\\xampp\\Gene\\IMEXporter\\include\\files\\' + $("#IMEXp_import_var-uploadFile-file").val();
$.post($_CFG_PROCESSORFILE, {"task": "csv_dupe", "file_name": file_name}, function(data) {
alert("success");
}, "json")
});
PHP
if ($task == "csv_dupe") {
$input_filename = $_REQUEST["file_name"];
$output_filename = $_REQUEST["file_name"];
$input_file = fopen($input_filename, 'r');
$output_file = fopen($output_filename, 'w');
$email_addresses = array();
// Read the header
$headers = fgetcsv($input_file, 0);
fputcsv($output_file, $headers);
// Flip it so it becomes name => ID
$headers = array_flip($headers);
// Read every row
while (($row = fgetcsv($input_file, 0)) !== FALSE)
{
$email = $row[$headers['email']];
// Do we already have this email address?
if (isset($email_addresses[$email]))
continue;
// Mark this email as being found
$email_addresses[$email] = true;
// Write it to the output
fputcsv($output_file, $row);
}
}
I cant seem to figure out why its not working and what I am doing wrong. Any ideas would appreciated.
EDIT *Fixed but new errors show*
I received some errors when the request returned.
Warning: fputcsv() expects parameter 2 to be array, boolean given in C:\server\xampp\htdocs\Gene\IMEXporter\include\IMEXporter_processor.php on line 45
Warning: array_flip() expects parameter 1 to be array, boolean given in C:\server\xampp\htdocs\Gene\IMEXporter\include\IMEXporter_processor.php on line 47 "success"