2016-12-28 3 views
0

두 개의 배열이 있습니다. 1 개가 비어 있고 다른 5 개가 있습니다. 나는 그들을 세고 그것을 보여주고 싶다.Ajax가 잘못된 결과를 반환합니다.

는이 같은 아약스 요청을 보내 : 나는 모두가 내가 5를 이해하지 못할하는 응답을받을 때

function countTrash() 
    { 
     $.ajax({ 
       type: "GET", 
       url: "count_trash_delete.php", 
       data: "action=1", 
       success: function(response){ 
          $("#badge3").html(response); 
         } 
     }); 
    } 




function countRemove() 
    { 
     $.ajax({ 
       type: "GET", 
       url: "count_trash_delete.php", 
       data: "action=2", 
       success: function(response){ 
          $("#badge2").html(response); 
         } 
     }); 
    } 

내 count_trash_delete.php이

if(isset($_GET['action'])) { 
    $action = 1; 
}else{ 
    $action = 2; 
} 

if($action === 1){ 

    $trash_arr = file_get_contents('trash_bots.json'); 
    $trash_arr = json_decode($trash_arr); 
    $number_of_trashed = count($trash_arr); 

    echo $number_of_trashed; 

}elseif($action === 2){ 

    $remove_arr = file_get_contents('remove_bots.json'); 
    $remove_arr = json_decode($remove_arr); 

    if(!empty($remove_arr)){   
    $number_of_removed = count($remove_arr);  
     echo $number_of_removed;   
    }else{ 
     echo 'Empty'; 
    } 
} 

것 같습니다.

답변

1

같은 일을하도록 페이지에 요청하는 것이므로 동일한 일을합니다. 이 문제 코드 :

if(isset($_GET['action'])) { 
    $action = 1; 
}else{ 
    $action = 2; 
} 

그것은 중요하지 않습니다 무슨 $_GET['action']는, 그 코드에이 전혀있을 경우 1$action을 설정합니다 그리고 거기에 당신이없는 경우 $action2으로 설정합니다. 항상 action을 전달하므로 페이지는 항상 동일한 작업을 수행합니다. 나는 그것을보고하지 않았다 믿지 못할 ..

if(isset($_GET['action'])) { 
    $action = (int)$_GET['action']; 
}else{ 
    $action = /*...some appropriate default number...*/; 
} 
+0

오 :

당신은 아마 $_GET['action']$action을 설정합니다. 고마워. – MHH

관련 문제