2010-07-26 3 views
3

사용자가 검색어를 입력하면 매개 변수가 jquery를 통해 전달되어야하고 결과를 얻은 후에 div 컨테이너에 결과가로드되어야하는 형식이 있습니다. 내가 jquery에 정통하지 않기 때문에 어떻게해야할까요?jquery를 통해 검색 매개 변수 전달

HTML :

//currently the data is being displayed on pageload: 
    $(document).ready(function() { 
    $("#bquote").load("quotes_in.php") 
    }); 

    $(".bsearch") 
    .keydown(function() { 
    //pass the parameter to the php page 

     //display in div #bquote 

    }); 


<!-- Begin Search --> 
     <div id="search" style="display:none;"> 
       <input type="text" class="bsearch" name="search" value="Search" /> 
     </div> 
<!-- End Search --> 

PHP [이용하여 OOP] :

if (isset($_POST["search"])) 
{ 
    $quotes->searchQuotes(); 
} 

PHP 클래스 :

$search = $_POST['search']; 

    $sql = "SELECT * FROM thquotes WHERE cQuotes LIKE '%" 
    . mysql_real_escape_string($search) ."%' ORDER BY idQuotes DESC"; 


    try { 

    $query = $this->_db->prepare($sql); 
    $query->execute(); 

    if(!$query->rowCount()==0) 
    { 
    while($row = $query->fetch()) 
    { 
    echo $this->formatSearch($row); 
} 

답변

3

내가 그런 식으로 할 것 :

$(".bsearch").keydown(function() { 
    //create post data 
    var postData = { 
    "bsearch" : $(this).val(), 
    "anotherParam" : "anotherValue" 
    }; 

    //make the call 
    $.ajax({ 
    type: "POST", 
    url: "yourAjaxUrl.php", 
    data: postData, //send it along with your call 
    success: function(response){ 
     alert(response); //see what comes out 
     //fill your div with the response 
     $("#bquote").html(response); 
    } 
    }); 
}); 

편집 :

을 로더를 넣어 주셔서은 여기를 확인해야합니다

http://api.jquery.com/category/ajax/global-ajax-event-handlers/

그리고이 로더 이미지를 보여 예 :

$("#loading").ajaxStart(function(){ 
    $(this).show(); 
}); 

아약스 호출이 완료되면 숨기기 위해 :

$("#loading").ajaxComplete(function(){ 
    $(this).hide(); 
}); 
+0

감사합니다.이 작동하지만, #bquote 컨테이너에 결과를 표시하려면 어떻게해야합니까? 또한 데이터를 가져오고 있기 때문에 백그라운드에서 데이터를 가져 오는 것이 아닙니다. 내가 로데르를 전시 할 곳은? – input

+0

당신은 지금 답변에있는 모든 것을 가지고 :) 편집 된 부분을 확인하십시오 ... – KakambaWeb

+0

감사합니다! 받아 들였다. :) – input

0

대신 $ _POST를 사용하여, 당신은 $ _GET이나 $ _REQUEST를 사용할 수 있습니다 다음과 같이 입력하십시오 :

var searchString = $(".bsearch").val(); 
$("#bquote").load("path_to_php_file.php?search="+searchString); 
당신이 아약스 경로를 아래로 이동하려면 6,

그런 다음, PHP에서,

$(".bsearch").keydown(function() { 

    // Get the current input value 
    var value = $(this).val(); 

    //pass the parameter to the php page 
    $.ajax({ 
     type: "POST", 
     url: "some.php", // replace this with the right URL 
     data: "bsearch=" + value, 
     success: function(msg){ 
      $("#search").html(msg); 
     } 
    });   
}); 

jQuery.ajax() 읽기 최대 ...

$_GET 
1

$_POST 

을 ... 대체 검색 창을 적절한 형태로 바꾸면 jQuery.serialize()

+0

감사합니다! #bquote 컨테이너에 결과를 표시하려면 어떻게해야합니까? – input