2012-07-23 2 views
1

클릭하면 Ajax 호출을하는 버튼에 이벤트 리스너를 연결하려고합니다.Ajax to PHP는 작동하지 않습니다.

필자는 test.php를 호출 할 파일에 일부 PHP를 별도로 설정했습니다. 라이브 jquery 함수를 사용하여 버튼에 대한 클릭 이벤트를 공격했습니다. ajax 함수는 페이지의 입력 요소 값을 test.php로 전달합니다. test.php 파일은 그 값을 사용하여 사용할 파일의 URL을 결정합니다. test.php 파일은 이메일과 일치하는 csv 파일을 제외합니다. csv 파일은 업로드 입력 요소를 통해 업로드됩니다.

여기 내 코드가 있는데, 현재 작동하지 않습니다. 피 들러 (Fiddler)는 요청이 서버로 전송되고 있지만 응답은 아직 나오지 않았 음을 보여줍니다.

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); 
} 

}

나는 왜 작동하지 않는지 그리고 내가 잘못하고있는 것이 무엇인지 파악하지 못하는 것 같습니다. 어떤 아이디어라도 감사 할 것입니다.

편집 * 고정하지만 새로운 오류가 요청이 돌아 왔을 때 나는 약간의 오차가받은 *

을 보여줍니다.

경고 : fputcsv()가 파라미터 2 C에서 주어진 배열 부울 것으로 기대 : \ 서버 \ XAMPP \ htdocs를 \ 유전자 \ IMEXporter \ 라인에 \ IMEXporter_processor.php 포함 45

경고 : array_flip()는 예상 매개 변수 1 배열 수, boolean C : \ server \ xampp \ htdocs \ Gene \ IMEXporter \ include IMEXporter_processor.php 줄에 47 "성공"

+0

, 마지막에 추가하려고하지 :로 json_encode 에코 ("성공을 PHP가 수동에서 "); – Tomer

+0

"echo json_encode ("success ");"를 추가했습니다. if 절의 끝에서 php의 마지막 괄호 바로 앞에 있지만 불행하게도 작동하지 않습니다. –

+0

jsonp에서 jsonp로 변경하려고 했습니까? 도메인 간 게시물처럼 보입니다. –

답변

0

here 사람을 찾았습니다. 파일의 끝에 도달 할 때

추가 라인) (파일을

ini_set('auto_detect_line_endings',TRUE); 

fgetcsv를 열기 전에 "거짓"의 부울 값을 반환한다.

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

참조 : 당신은 더 PHP에서 어떤 대답을 반향하는

관련 문제