0

Using this to return a list of items that match the search term:

if (mysql_num_rows($result) > 0){
    while($row = mysql_fetch_object($result)){
        $string .= "<br><b>".$row->container."</b>"."  --- ";
    $string .= "  yard: ".$row->lot."";
    $string .= "  -- latitude: ".$row->lat."";
    $string .= "  -- longitude: ".$row->lng."<br />";
        $string .= "<br/>\n";
    }
}

...and then this to display...

function ajax_search() {
    $("#search_results").show();
    var search_val = $("#search_term").val();
    $.post("find.php", {
        search_term: search_val
    }, function (data) {
        if (data.length > 0) {
            $("#search_results").html(data);
        }
    })
}
$(document).ready(function () {
    $("#search_results").slideUp();
    $("#search_button").click(function (e) {
        e.preventDefault();
        ajax_search();
    });
    $("#search_term").keyup(function (e) {
        e.preventDefault();
        ajax_search();
    });
});

but I'm stumped on how to make these items clickable so I can send lat and lng to a map.

Sorry I'm both new to this and new to Stackoverflow.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143

1 Answers1

0

I'm sure about the server side syntax, please correct it if I made any syntactical mistakes.

It is easy if you an do minor changes to the result html, like wrap each item in a div

$string .= "<div class='item' data-latitude='".$row->lat."' data-longitude='".$row->lng."'><b>".$row->container."</b>"."  --- ";
$string .= "  yard: ".$row->lot."";
$string .= "  -- latitude: ".$row->lat."";
$string .= "  -- longitude: ".$row->lng."<br />";
$string .= "</div>";

then

$(document).ready(function(){ 
$("#search_results").slideUp(); 
$("#search_button").click(function(e){ 
    e.preventDefault(); 
    ajax_search(); 
}); 

$("#search_term").keyup(function(e){ 
    e.preventDefault(); 
    ajax_search(); 
}); 

$("#search_results").on('click', '.item', function(){
    var $this = $(this);
    var lat = $this.data('latitude');
    var long =$this.data('longitude'); 
    alert(lat + long)
})

}); 
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • May I suggest wrapping in anchors rather than divs, so that they will be "clickable" via the keyboard for users who can't use a mouse or other pointing device? – nnnnnn Apr 26 '13 at 00:40
  • @nnnnnn that should be fine, then the click handler have to return false – Arun P Johny Apr 26 '13 at 00:42