2012-01-18 2 views
0

다음 데이터 세트를로드하고 Ajax 호출 후 데이터를 제거하려고합니다. 하지만 아작스는 한 번만 일할 수있었습니다. "더 많은 인용문 가져 오기"를 클릭하면 아무 일도 일어나지 않습니다.ajax는 한 번만 작동 한 다음 클릭 후 중지됩니다.

사이트 : http://kdevs.site40.net/#more

아약스 (자세한 시세 클릭) :

<?php 

    require("inc/connect.php"); 
    $start = $_GET['start']; 
    $limit = $_GET['limit']; 

    $sql = "SELECT * FROM entries ORDER BY id DESC LIMIT ".$start.", ".$limit; 
    $result = mysql_query($sql, $link) or die("Error in query: ".mysql_error()); 

    $data = array(); 

    while($row = mysql_fetch_array($result)) { 
     array_push($data, $row); 
    } 

    echo(json_encode($data)); 
?> 

나는 그것이 PHP 때문에하지 알고 : http://kdevs.site40.net/quotes.php?start=0&limit=10 작품

function getQuotes(start, limit) { 
      //Clear the recent list element 
      $(".recent-list").html(""); 
      $.ajax({ 

       url: "quotes.php?start=" + start + "&limit=" + limit, 
       success: function (data) { 
       ata = jQuery.parseJSON(data); 
       console.log("success"); 
       //Data should now be a javascript array of objects. 
       for (i = 0; i < data.length; i++) { 
        var newElem = jQuery("<li></li>").attr('id', data[i].id).addClass('quote-wrap-group'); 
        newElem.append("<div class='quote'><p>" + data[i].quote + "</p></div>"); 
        $(".recent-list").append(newElem); 
       } 
       } 
      }); 
      } 

       $("#more").click(function() { 
       var currentIndex = 0; 
       getQuotes(currentIndex = currentIndex + 10, 10); 
       currentIndex += 10; 
       }); 

PHP는 JSON을 반환합니다.

내가 뭘 잘못하고 있니?

+1

조회에 SQL 주입을 앓고. http://en.wikipedia.org/wiki/SQL_injection을 읽고 수정하십시오. – Kenaniah

+0

^알아요. 전에 이것을 클라이언트에게 보내면 고칠 것입니다 ... – nowayyy

답변

2

버튼을 클릭 할 때마다 currentIndex가 0으로 재설정되므로 기본적으로 항상 처음 10 개의 설명을로드합니다.
var currentIndex = 0;를 제거하고 클릭 이벤트 기능

+0

당신이 먼저 대답했습니다, 고마워요! 코드를 수정/개선하기 위해 할 수있는 일이 있습니까? – nowayyy

1

대충 눈에 문제가 여기에 제안 이외의 추가 :

$("#more").click(function() { 
    var currentIndex = 0; 
    getQuotes(currentIndex = currentIndex + 10, 10); 
    currentIndex += 10; 
}); 

당신이 당신의 현재 인덱스를 재설정 #more 0에를 클릭 할 때마다; currentIndex을 클릭 핸들러 외부에 선언하고 재설정하지 마십시오.

+0

OMG 고마워요! 너무 간단! 내 코드에서 고정/더 나은 다른 것이 있습니까? – nowayyy

1
는 클릭 처리기 밖으로 currentIndex 초기화를 이동

당신은 변화에 두 번 추가 :

var currentIndex = 0; 
var count = 20; 
$("#more").click(function() { 
    getQuotes(currentIndex, count); 
    currentIndex += count; 
}); 
+0

답변을 업데이트했습니다. – scessor

+0

당신은 결코 따옴표를 얻지 못할 것입니다. 0 -> 10 – paislee

+0

@paislee : thx, 제 답변을 업데이트했습니다. – scessor

관련 문제