2016-11-08 6 views
0

jQuery AJAX를 사용하여 투표 시스템을 수행하고 있습니다. div (투표 할 때)을 클릭하면 중간 파일 (이 경우 up_vote.php)으로 데이터가 전송 된 다음 div가 새 데이터로 업데이트됩니다. 결과 = 2 (변수 더하기 1)를주는 것은 완벽하게 작동합니다.jQuery AJAX 투표 시스템

두 번째 단계에서 나는 다른 div (#results 페이지의 다른 사이트)에 투표 결과를 표시하려고합니다. 문제는 div가 새로 고쳐 지지만 결과가 잘못되어 2 대신 1을 표시합니다 (변수가 전송되지 않았거나 결과를 받기 전에 div가 새로 고쳐진 것처럼 보임).

나는
<?php 
    $id_field = $_POST['id_field']; 
    echo 'Result='; 
    echo $id_field + 1; 
?> 

이 더 많은 경험을 가진 사람이 나에게 설명 할 수

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<link type="text/css" rel="stylesheet" href="style.css" /> 
<script type="text/javascript" src="myjquery.js"></script> 
</head> 
<body> 
    <script type="text/javascript"> 
     $(function(){ 
      $(".n_vote").click(function() { 
       var id_field = $(this).attr("id"); 
       var parent = $(this); 

       $.ajax({ 
        type: "POST", 
        url: "up_vote.php", 
        data: { 
         id_field: id_field, 
         name: name 
        }, 
        cache: false, 
        success: function(html) { 
         //Show the results in the same div 
         parent.html(html); 
         //Show the results in another div, #results 
         $("#results").load("up_vote.php"); 
        } 
       }); 
       return false; 
      }); 
     }); 
    </script> 

    <div class="vote_system"> 
     <div class="n_vote" name="up" id="1">Clic here to show the result</div> 
    </div> 
    <!-- Correct result=2 --> 
    ______________________________ 

    <br> 
    Show the result in a another div: 
    <br> 
    <div class="results" id="results"> 
    </div> 
    <!-- Incorrect result=1 --> 
</body> 
</html> 

up_vote.php 사람이 시도하고자하는 경우에 전체 코드 ...

Index.html을 떠나 두 번째 div가 올바른 결과를 표시하지 않는 이유는 무엇입니까? 무슨 일이 일어나고 어떻게 해결합니까?

답변

2
대신 (당신이 얻을없이 매개 변수를 통해 URL을로드하기 때문에 obviousely 통과 $의 id_field가로 설정하고 비어 있지) up_vote.php로드 $("#results").load("up_vote.php");를 사용

그래서 당신은 당신이 한 대신 2
가지고 있음을 정상이야 서버에서 반환 된 동일한 성공 html을 사용하여 두 번째 div에 표시합니다 (예 : $("#results").html(html);

).