douji8033 2012-07-23 10:43
浏览 35

Ajax到php不会工作

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"

  • 写回答

1条回答 默认 最新

  • douli4852 2013-03-12 17:08
    关注

    Found someone with a similar problem here.

    Add line before opening a file

    ini_set('auto_detect_line_endings',TRUE);
    

    fgetcsv() is returning a boolean value of "false" when it reaches the end of the file. From php-manual:

    fgetcsv() returns NULL if an invalid handle is supplied or FALSE on other errors, including end of file.
    

    references:

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度